This question already has answers here:
PHP: add seconds to a date
(9 answers)
Closed 8 years ago.
I am trying to figure out how to easily add minutes to a timestamp in php.
I can do this easily in SQL such as select now() + interval '60 minutes' or using the DATEADD function, however I am not sure how to easily do this in PHP.
I have a date variable that I call as:
$date = date("Y-m-d H:i:s");
And now I just need to add or subtract time from this.
Can anybody help?
Thanks!
You could use strtotime() like this:
$date = date('Y-m-d H:i:s', strtotime('now +60 minutes'));
Which would give you the date 60 minutes in the future formatted the way you'd like.
If you have the timestamp is like this
$tmt += 2 * 60 * 1000;
$date= date("Y-m-d H:i:s", $tmt);
Try this
Related
This question already has answers here:
Adding minutes to date time in PHP
(13 answers)
Closed 1 year ago.
Is there a way of getting the next hour's 15th minutes from given time?
For Example
if input is 2021-08-26 12:00:37 then output should be 2021-08-26 12:15:00
if input is 2021-08-26 12:30:37 then output should be 2021-08-26 13:15:00
Use Carbon
It is installed by default in Laravel.
$date = Carbon::createFromFormat('Y-m-d H:i:s', '2021-08-26 12:00:37');
$newDate = $date->addMinutes(15)->format('Y-m-d H:i:s');
You can convert the date to a timestamp and then add it the equivalent of 15 minutes in seconds (15 * 60) and then convert it back to a date
I think this is what you looking for...
$start = '2018-05-21 20:24:45';
echo date('Y-m-d H:i:s',strtotime('+15 minutes',strtotime($start)));
This question already has answers here:
Find difference between two datetimes and format at Y-m-d H:i:s
(7 answers)
time difference in HH:MM:SS format
(2 answers)
Closed 5 years ago.
I am trying to find the time difference between two time values in the format H:mm:ss using PHP.
When I tried with the following code, I'm getting the difference 01:00:20 instead of 00:00:20. What's wrong with my code?
$start_time = strtotime("0:17:14");
$end_time = strtotime("0:17:34");
$diff = $end_time - $start_time;
echo date('H:i:s', $diff);
Your $diff variable is not a timestamp, it's a duration/interval. The date() function is intended to format timestamps, and won't properly handle intervals like you're expecting.
Instead, try using the DateTime class to read your timestamps, and turn the difference between them into a DateInterval using DateTime::diff(). You can then use DateInterval::format to get the output you want.
Something like this should work:
$start_time = new DateTime("0:17:14");
$end_time = new DateTime("0:17:34");
$diff = $end_time->diff($start_time);
echo $diff->format('%H:%I:%S');
This question already has answers here:
Does PHP time() return a GMT/UTC Timestamp?
(5 answers)
Closed 7 years ago.
I have an event date & time stored in my database that is being saved as a PHP unix timestamp. Im trying to check if today is greater than the event date and time.
The problem is - i'm trying to check it using local time (America/New_York), but im getting 2 different time zones. time() is displaying in EST and my database is displaying in UTC
Is there any way to check it correctly?
My Event Date from database:
1450447200 (December 18, 2015 2:00pm)
Im trying to compare it with php time()
am I maybe doing this wrong?
*UPDATE - ANSWER*
as per this answer I ended up doing this:
$utc_str = gmdate("M d Y H:i:s", time());
$today = strtotime($utc_str);
$event_datetime = $Event_timestamp;
date_default_timezone_set("America/New_York");
$utc_str_event = gmdate("M d Y H:i:s", $event_datetime);
$event_date = strtotime($utc_str_event);
if($today >= $event_date){
//Do Something
}
You probably have wrong date on server(s). time() always should return epoch (epoch = UTC).
btw. saving date as int on database it isn't best practice
I don't know if this is correct but have you tried using date_default_timezone_set
This question already has answers here:
php string in a date format, add 12 hours
(4 answers)
Closed 8 years ago.
I'd like to take a number of hours imputed in a text box and get a timestamp back so I can create a countdown timer.
The countdown is fine so ignore how that is done etc. But whats the best way to get a timestamp '48' hours from now. For example user has entered 48?
$hours = 48;
$timestamp = (new DateTime())->modify("+{$hours} hours")->format('U');
You can create a timestamp like this:
<?php
strtotime(date('d-m-Y H:i:s') . "+ 48 hours");
?>
Your looking for strtotime. For example, you can just use strtotime('+48 hours'), and it will return a unix timestamp for that time.
This question already has answers here:
How to add 5 minutes to current datetime on php < 5.3
(7 answers)
Closed 8 years ago.
I have a mysql table which contains a field named time and has the "time" format. I added the first line into the table manually and its time is "14:55:00".
Now, in my PHP page, users will fill a form and then submit it. Here, I want my php to assign the last register's time value into a variable like $time (i can do that). Then I want this:
$time=$time + 5 minutes
How can I do that?
If you want to update your sql table, you can use the DATE_ADD function:
UPDATE table SET time = DATE_ADD(time, INTERVAL 5 MINUTE)
If you want to add 5 minutes to a timestamp in PHP you can use the function strtotime:
$time = $time + strtotime("+5 minutes");
If your time is a string (ie in format 14:55:00), you can do the following:
$timeAsString = "14:55:00";
$timestamp = strtotime("+5 minutes", strtotime($timeAsString));
$time = date("H:i:s", $timestamp);
echo $time;
$dateMinutes= date("i")+5;
and use the make time function from php