Date difference in PHP to update the mysql db - php

I am trying to log the time difference in a mysql-field and when I try on screen the values are correct. After submitting the field to the database the field is empty.
Where do I go wrong?
The field is declared in mysql as timestamp and format current_timestamp.
The code:
$strStart = date("Y-m-d H:i:s");
sleep(5);
$strEnd = date("Y-m-d H:i:s");
$dteStart = new DateTime($strStart);
$dteEnd = new DateTime($strEnd);
$dteDiff = $dteStart->diff($dteEnd);
//$calc = $dteDiff->format("YYYY-%M-%D %H:%I:%S");
$calc = $dteDiff->format("%Y%Y-%M-%D %H:%I:%S");
echo "Difference: " . $calc;
I am submitting the $calc to the db.

As MySQL dates have a minimum value of '1000-01-01 00:00:00', you cannot store this value ('0000-00-00 00:00:05 for 5 seconds) as a date/time value.... nor should you, because it isn't a date/time..... date/time is a fixed point in time, not an interval, which is the period between 2 date/times. There is no specific datatype for a date interval.
I would suggest storing the actual date/time value rather than the interval, and only calculating the interval when you need to do so.
Alternatively, if you must store the interval itself, then store a number of seconds as a float rather than trying to force it into a formatted an inappropriate datatype; so store 5.0 seconds instead.

Related

Determine time difference between now and stored date and time

in my sql table I save the date and time of a certain action, how can I print it via php on the contrary, or how long ago did it take place?
for example
|date |
|05-28-2020 17:12:05 |
and at 17:30:05 it will be written on my php page
"18 minutes ago"
Load the date from the database using a mysqli_query and store the result in $original_date.
Create a date object from the string in the database.
$old_date = new DateTime($original_date);
Get the current date in the same format and store it in another variable.
$current_date = new DateTime(date("m-d-Y H:m:s"));
Get the difference between the two dates.
$difference = $old_date->diff($current_date);
Convert the difference to minutes and display message.
echo($difference->m . "minutes ago");
Here is the full code for you to use:
# Get database value and store in $original_date
$old_date = new DateTime($original_date);
$current_date = new DateTime(date("m-d-Y H:m:s"));
$difference = $old_date->diff($current_date);
echo($difference->m . "minutes ago");

DateTime current date + 2 mins compared to stored datetime

I'm trying to compare these 2 DateTimes.
I'm storing one in a database that is stored whenever a new row is inserted.
The other one is the current DateTime, but I'm trying to add two minutes to it and then compare it.
My current code:
$time = $cached_data["last_update"];
$date = new DateTime ('now');
$datef = $date->format('Y-m-d H:i:s');
// Check if the cached data has expired and if it has then update the data
if($datef > ($time)) {
echo "updated";
}
Echoing both the $datef and the $time gives:
$datef: 2016-04-06 00:23:43
$time: 2016-04-05 18:21:03
and the script echoes out "updated".
I realize the time on my system and the time of my database are different, but how would I add the two minutes to the $datefand would this work?

Get the date from a time string

Is there a way to convert an input time string (ex: 01:13) to a Zend date object, so that I store it later in a timestamp column in a Mysql database.
Examples:
If the current datetime is 2013-07-15 17:33:07 and the user inputs 18:05 the output should be 2013-07-15 18:05:00.
If the current datetime is 2013-07-15 17:33:07 and the user inputs 02:09 the output should be 2013-07-16 02:09:00. Notice that since the time entered was lower than the current time, so it was treated as tomorrows time.
I simply want to get the next point in time that satisfies the entered time. I'm open for solution using plain PHP or Zend_Date.
I think you should compare the current time with the time entered by the user and create a DateTime object of either "today" or "tomorrow". DateTime accepts strtotime() relative time parameters.
Quick hack. Works as of today, 15.07.2013 23:58 local time:
$nextTime = new DateTime('today 18:10');
if ($nextTime < new DateTime('now')) { // DateTime comparison works since 5.2.2
$nextTime = new DateTime('tomorrow 18:10');
}
echo $nextTime->format('d.m.Y H:i:s');
here is working example for you just add your dynamic variable to check date with user inputs
You can use mktime function to manage your date.
$input_date = date("Y-m-d H:i:s",mktime(18,05,0,date("m"),date("d"),date("Y")));
echo "current time".$current_time = date('Y-m-d H:m:s');
echo "<br>User input is ".$input_date;
if(strtotime($current_time) > strtotime($input_date)){
$input_date = date("Y-m-d H:i:s",mktime(18,05,0,date("m"),date("d")+1,date("Y")));
echo "in";
}else{
// nothing to do
}
echo "<br> result->".$input_date;
i hope it will sure solve your issue

comparing datetime is 1 hour ahead

I'm trying to count the rows with a datetime less that 10 minutes ago but the current time its being compared to seems to be 1 hour ahead so Imm getting 0 results, if I go into my table and put some fields forward an hour then I get results.
Getting results:
$stmt = $db->query('SELECT check_log FROM members WHERE check_log >= DATE_SUB(NOW(), INTERVAL 10 MINUTE)');
$row_count = $stmt->rowCount();
echo $row_count.' Members online.';
The datetime of the field of of typing this is 2013-07-11 16:54:12 and I'm getting no results but if I manually change the date time to 2013-07-11 17:54:12 I get 1 result the datetime was input seconds ago using:
$date = date("Y-m-d H:i:s");
The 17:54:12 is my local time and 16:54:12 seem to be my servers time, is my compare trying to look into the future or is it using my local time as a reference?
PHP and MySQL don't agree on the current timezone.
Pass the desired time in as a literal from PHP to SQL instead of using NOW().
Always store date times in php's timezone.
One function you can particularly make use of is strtotime.
$now = strtotime("now"); // current timestamp
$hour_later = strtotime("+1 hour"); // hour later
$now = date("Y-m-d H:i:s", $now);
$hour_later = date("Y-m-d H:i:s", $hour_later);

How can I identify the time after user update?

How can I identify the time passed after an user updated his account in my MySQL database? I have a timestamp in my MySQL table (to store user update time) so now how can I identify the time passed from last user update, using PHP?
As example:
User last update time: 2012-04-08 00:20:00;
Now: 2012-04-08 00:40:00;
Time passed since last update: 20 (minutes) ← I need this using PHP
If you have the data on
$last_update_time = '2012-04-08 00:20:00';
$now = time(); //this gets you the seconds since epoch
you can do
$last_update_since_epoch = strtotime($last_update_time); //this converts the string to the seconds since epoch
aand... now, since you have seconds on both variables
$seconds_passed = $now - $last_update_since_epoch;
now, you can do $seconds_passed / 60 to get the minutes passed since the last update.
Hope this helps ;)
In pseudo-code:
$get_user_time = mysql_query("SELECT timestamp FROM table");
$row = mysql_fetch_array($get_user_time);
$user_time = $row['timestamp']; //This is the last update time for the user
$now_unformatted = date('m/d/Y h:i:s a', time());
$now = date("m/d/y g:i A", $now_unformatted); // This is the current time
$difference = $now->diff($user_time); // This is the difference
echo $difference;
diff() is supported in >= PHP 5.3. Otherwise you could do:
$difference = time() - $user_time;
$secs=time()-strtotime($timestamp);
would give u number of seconds before it was updated and then you can convert this seconds into your needed time format like
echo $secs/60." minutes ago";
why not do it with mysql:
select TIMEDIFF(NOW(),db_timestamp) as time_past

Categories