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?
Related
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");
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.
I have 2 tables in my SQL Database. One is called dungeons and the other one is called dungeonruns.
In the dungeons table there is one row called time it's the SQL time format 00:00:00. In the dungeonruns table is a row called stoptime which is in the timestamp format 0000-00-00 00:00:00.
I want my PHP script to get the time from dungeons, add it to the current time and then save it in the stoptime row.
This is what I have tried:
$stoptime = date('Y-m-d H:i:s') + $time;
//$time is the time from the DB and its the 00:00:00 format.
$mysqlDateTime = '2015-09-01 00:00:00';
$timeToAdd = '12:30:00';
$timeParts = explode(':', $timeToAdd);
$intervalString = sprintf('PT%dH%dM%dS', $timeParts[0], $timeParts[1], $timeParts[2]);
$stopTime = (new DateTime($mysqlDateTime))->add(new DateInterval($intervalString))->format('Y-m-d H:i:s');
Demo
Basically, to add that time to that datetime value you need to turn it into a DateInterval which you can then add to a DateTime object.
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
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