Compare date() to timestamp PHP - php

Is there a way that I can compare a day with date()on php and timestamp() on db like this
example:
date(); has this value 2021-11-02
timestamp() has this value 2021-11-02 11:00:52
here's my code
$current_date = date("Y-m-d");
$fetch_user_by_number = "SELECT id,transfer_amount,number FROM `transfer_wallet` WHERE `transfer_number`=:number AND `transaction_date` ORDER BY `transaction_date` DESC limit 1";
What I am trying to do is create a push notification on my App that whenever a new data arrived at my db i will push notify the user on my application.
I am just kind of lost on the comparison of date stuff. Any logic or idea will be very appreciated . Thank you.

Try like this
SELECT `id`, `transfer_amount`, `number`
FROM `transfer_wallet`
WHERE `transfer_number`= :number
AND `transaction_date` >= now()
ORDER BY `transaction_date` DESC limit 1

Related

Adding days to a Date (retrieved from MySQL database) in PHP

I am trying to do, what I assume is, an easy task of adding days to a date.
I have a date stored in a MySQL table, in a column called meta_date, with the type of DATE (A date, supported range is 1000-01-01 to 9999-12-31)
I retrieve this date from the database as follows:
$thisId = 1;
$dateQuery = mysqli_query($connection, "SELECT * FROM `sometable` WHERE `id` = '$thisId'");
$fetchDate = mysqli_fetch_assoc($dateQuery);
$theDate = $fetchDate['meta_date'];
Now I add a number of days to this date.
$newDate = date("Y-m-d", strtotime($theDate . " + 7 days"));
Next I put it back inside the database with an UPDATE query.
$editDate = mysqli_query($connection, "UPDATE `sometable` SET `meta_date` = '$newDate' WHERE `id` = '$thisId'");
However the date always returns as 0000-00-00 after the update.
Am I missing something here to do with the way the date is handled in PHP?
edit: The data I first retrieve from the database (into $theDate) is "2016-11-30".
You can use Mysql's built in function DATE_ADD()
Syntext
DATE_ADD(date,INTERVAL expr type) Where date is a valid date expression and expr is the number of interval you want to add.
For your case
UPDATE sometable
SET `meta_date` = DATE_ADD(`meta_date` , INTERVAL 7 DAY)
WHERE `id` = '$thisId';

MySql Query for Current Date

I have s MySQL Query where I want to pull data from my database but base it on the current month information
FROM lbs_trace_etrack WHERE (lbs_agent = '$slfirstname' AND DATE (lbs_date) = CURDATE()) ORDER BY lbs_date DESC LIMIT 0,50");
This string pulls out the information for the current day.
I have also tried the below string but get no results from it:
FROM lbs_trace_etrack WHERE (lbs_agent = '$slfirstname' AND MONTH(lbs_date) = (MONTH(NOW()) AND YEAR(lbs_date) = YEAR(NOW())
My table date format is as follow 2016-08-02
Or using PHP variables as so:
<?php
$start = date('Y-m-01'); // FIRST DAY OF CURRENT MONTH
$end = date("Y-m-t", strtotime(date("Y-m-d"))); // LAST DAY OF CURRENT MONTH
$sql = "SELECT * FROM lbs_trace_etrack WHERE lbs_agent = '".$slfirstname."' AND (lbs_date BETWEEN '".$start."' AND '".$end."')";
?>
I have done the following and it works
FROM lbs_trace_etrack WHERE lbs_agent = '$slfirstname' AND MONTH(lbs_date) = MONTH(CURDATE()) AND YEAR(lbs_date) = YEAR(CURDATE()) ORDER BY lbs_date ASC, lbs_time ASC
Thanks to all and Tijo for guidance
Assuming lbs_agent is a DATE field type as mentioned in comments, you could do this (note I am just showing the pertinent date part of your WHERE clause):
WHERE lbs_agent >= DATE_FORMAT(NOW() ,'%Y-%m-01')
It is important that you do not use a function call on the left (field definition) side of the WHERE comparison, as you will then not be able to leverage any index on that field. Doing this would require a full table scan with MySQL performing the function on this field for every row in the table such that the comparison can be made.
Feel free to use MySQL functions for the comparison value, as those would be calculated just once when the query is being planned. You would then be able to use an index on the field for quickly filtering the rows in the table that meet the criteria. From a query execution standpoint, this is basically that same as if your query has this WHERE clause:
WHERE lbs_agent >= '2016-08-01'
This is as compared to the examples in your question which would be executed as:
WHERE DATE(lbs_date) = '2016-08-03'
and
WHERE MONTH(lbs_date) = 8 AND YEAR(lbs_date) = 2016
Both of these would require full table scan since the values derived from the field are not able to be determined until the row is scanned.
You could try to extract the month, such as EXTRACT(MONTH from NOW())
you can use following code if it timestamp
MONTH(CURDATE())

Query to select the most recent time in database

Guys I have this php code to return the current time:
$time = date('m/d/Y h:i:s a', time());
I saw this time to my database using this code :
$query = "INSERT into `time` VALUES('', '$time')";
And the time row values update every transaction.
The question here is how I could return the most recent time?
I think i need an sql statement that return the time descendingly but I don't know what is it, so any help?
You can do a simple select, depending on what you want to do:
SELECT * FROM time ORDER BY <time> DESC LIMIT 1
I don't know the name of the column so I wrote <time>.
You just need to do an ORDER BY and a LIMIT 1:
SELECT `yourTimeColumn`
FROM `time`
ORDER BY `yourTimeColumn` DESC
LIMIT 1;
In addition, you could do your INSERT with the current time by just doing:
INSERT INTO `time` VALUES ('', NOW())
if your date is not in datetime format then It needs to be converted to datetime first.
Hope the following will help you.
SELECT * FROM time ORDER BY CONVERT(datetime, column) DESC LIMIT 1
column is the name of the time column.

Is there a way to return timestamp after updating timestamp using now()

I have query like:
UPDATE orders SET order_state = $state_id, order_confirmed = now() WHERE order_id = $id LIMIT 1
I'm just wondering if there was a function in php or sql that returned the updated timestamp kind of like insert id.
To avoid second query, use php's DateTime Object and format it, instead of Mysql's NOW()
$now = new DateTime;
UPDATE orders SET order_state = $state_id, order_confirmed = $now->format('Y-m-d H:i:s') WHERE order_id = $id LIMIT 1
How about
SELECT now() FROM orders LIMIT 1
or
SELECT now() FROM dual

SQL Items within the Last Day

In my code, I am trying to find items in an activities table that are within the last day. This query is not returning any results, are there any problems with it? Is there a better query?
$curday = time() - (24*3600);
$query = "SELECT * FROM activities WHERE userid = '$userid' AND 'timestamp' > '$curday'";
There are two choices here, you can get and format the date through PHP or use SQL language to do it. I prefer to do it within the SQL, it also allows me to use the same query in a MySQL client.
This question is essentially the same thing: MySQL SELECT last few days?
This would be the new query:
$query = "SELECT * FROM activities WHERE userid = '$userid' AND 'timestamp' > DATE_ADD(CURDATE(), INTERVAL -1 DAY)";
you can try with unix function 'mktime' to get value of yesterday ..
as
$curday = mktime(0,0,0,date("m"),date("d")-1,date("Y"));
for reference
if your database will mysql only then you can extract yesterday in sql itself..
SELECT * FROM activities
WHERE userid = '$userid'
AND timestamp > DATE_SUB(CONCAT(CURDATE(), ' 00:00:00'), INTERVAL 1 DAY)
one more thing if timestamp is your column name don't put this column inside single quote ..
What you can use is DATE_SUB. This can be used as follows
SELECT * FROM activities
WHERE userid = '$userid'
AND timestamp > date_sub(current_date, interval 1 day)
This way you don't need to work with current date in PHP
in Informix it would be (TODAY - 1) if the column is type DATE

Categories