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.
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");
So I have a database entry that update the date/time in yyyy-mm-dd hh:mm:ss.
Now I want to check, if there is a space inbetween the the database and the actual time from 60 minutes. How can I do that?
Example:
DB: 2020-02-14 10:00:00
Time now: 2020-02-14 11:01:00
Do something
DB: 2020-02-14 10:00:00
Time now: 2020-02-14 10:59:00
Do nothing
You can use something like this:
$t1 = strtotime( '2006-04-14 11:30:00' );
$t2 = strtotime( '2006-04-12 12:30:00' );
$diff = round(($t1 - $t2) / 3600);
In MySQL, you can do date arithmetics:
update mytable
set mydatetime = now()
where mydatetime <= now() - interval 1 hour and id = ?
The where clause filters on record whose column mydatetimeis more than one hour old. You did not tell what you want to do , so I assumed an update query, that resets the date/time to the current date/time.
This assumes that you want to update the timestamp of a given record, not accross the whole table, hence condition id = ?, which you can adapt - or remove - for your use case.
Hello Schmaniel at first i think you should use Carbon()
https://carbon.nesbot.com/docs/ to get the right results. It's a great way to work with timeformats and timestamps.
$now = Carbon::now();
$dbtime = Carbon::createFromFormat('Y-m-d H:i:s', '1975-05-21 22:10:22');
$totalDuration = $now->diffForHumans($dbtime);
dd($totalDuration);
$currentDate = new DateTime();
$databaseDate = new DateTime($dateFromDatabase);
$interval = $datetime1->diff($datetime2);
Then you can check using the $interval variable
You can use mysql TIMESTAMPDIFF like this:
SELECT TIMESTAMPDIFF(HOUR,'2003-02-01','2003-05-01 12:05:55');
you can use one of the follwing units:
MICROSECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH,
QUARTER, or YEAR.
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'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?
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);