I would like to add 24 hours to the timestamp for now. How do I find the unix timestamp number for 24 hours so I can add it to the timestamp for right now?
I also would like to know how to add 48 hours or multiple days to the current timestamp.
How can I go best about doing this?
You probably want to add one day rather than 24 hours. Not all days have 24 hours due to (among other circumstances) daylight saving time:
strtotime('+1 day', $timestamp);
A Unix timestamp is simply the number of seconds since January the first 1970, so to add 24 hours to a Unix timestamp we just add the number of seconds in 24 hours. (24 * 60 *60)
time() + 24*60*60;
Add 24*3600 which is the number of seconds in 24Hours
Unix timestamp is in seconds, so simply add the corresponding number of seconds to the timestamp:
$timeInFuture = time() + (60 * 60 * 24);
You could use the DateTime class as well:
$timestamp = mktime(15, 30, 00, 3, 28, 2015);
$d = new DateTime();
$d->setTimestamp($timestamp);
Add a Period of 1 Day:
$d->add(new DateInterval('P1D'));
echo $d->format('c');
See DateInterval for more details.
As you have said if you want to add 24 hours to the timestamp for right now then simply you can do:
<?php echo strtotime('+1 day'); ?>
Above code will add 1 day or 24 hours to your current timestamp.
in place of +1 day you can take whatever you want, As php manual says strtotime can Parse about any English textual datetime description into a Unix timestamp.
examples from the manual are as below:
<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>
$time = date("H:i", strtotime($today . " +5 hours +30 minutes"));
//+5 hours +30 minutes Time Zone +5:30 (Asia/Kolkata)
Related
I have the following code which outputs 20191027 as a result.
If I amend the 2nd line (i.e. set timezone to Auckland), it gives me the result 20191028. Why is this?
date_default_timezone_set("Europe/London");
#date_default_timezone_set("Pacific/Auckland");
$date_format = 'Ymd';
$day = "Sunday 4 week ago";
$start_of_the_week = strtotime($day);
$next_day = $start_of_the_week + (60 * 60 * 24 * 1);
$next_day = date($date_format, $next_day);
echo $next_day;
Check 2 outputs:
https://3v4l.org/A7ppT (20191027)
https://3v4l.org/Mfto3 (20191028)
In Europe/London timezone...
DST ended on Sun 27-Oct-2019 at 02:00:00 A.M. when local clocks were
set backward 1 hour
Keep in mind that that strtotime operates on unix timestamps where there is no concept of DST but the date function adjusts the unix timestamp to local timezone when formatting it. So:
$start_of_the_week = strtotime("Sunday 4 week ago"); // $start_of_the_week is some unix timestamp
echo date("Y-m-d H:i:s", $start_of_the_week); // 2019-10-27 00:00:00 Europe/London time
$next_day = $start_of_the_week + (60 * 60 * 24 * 1); // you're adding 24 hours to a unix timestamp
echo date("Y-m-d H:i:s", $next_day); // 2019-10-27 23:00:00 Europe/London time
And 2019-10-27 23:00:00 is still a Sunday. The solution is to add days instead of hours:
$next_day = strtotime("+1 day", $start_of_the_week); // 2019-10-28 00:00:00
As discussed in the comments, the issue is with Europe/London finishing daylight savings time on that day 4 weeks ago, so adding 24 hours to that time only took you forward by 23 hours. You can avoid issues like this by using DateTime objects and only working with days:
$date_format = 'Y-m-d H:i:s';
$day = "Sunday 4 week ago";
date_default_timezone_set("Europe/London");
$date = new DateTime($day);
$date->modify('+1 day');
echo $date->format($date_format) . "\n";
date_default_timezone_set("Pacific/Auckland");
$date = new DateTime($day);
$date->modify('+1 day');
echo $date->format($date_format) . "\n";
Output:
2019-10-28 00:00:00
2019-10-28 00:00:00
Demo on 3v4l.org
You can specify the timezone directly to the DateTime constructor too:
$date_format = 'Y-m-d H:i:s';
$day = "Sunday 4 week ago";
$date = new DateTime($day, new DateTimeZone("Europe/London"));
$date->modify('+1 day');
echo $date->format($date_format) . "\n";
$date = new DateTime($day, new DateTimeZone("Pacific/Auckland"));
$date->modify('+1 day');
echo $date->format($date_format) . "\n";
Demo on 3v4l.org
there is a difference between every time zone.
For Example
"India is 10 hours and 30 minutes ahead of Washington, DC, USA". If the echo the time of these time zones, it would end in giving different results.
In your case "Auckland, New Zealand is 13 hours ahead of London, UK", hence it is giving different O/P's
Hope this resolves your answer to the question :)
i want get only day, month, year, hour not include minute, second and subtraction it 5 hours. I search but result include minute and second
Ex: now 13/10/2015 18h and i want time now - 5 hours is 13/10/2015 13h
$dateTime = new DateTime();
$dateTime->modify('-5 hours');
echo $dateTime->format('d/m/Y h\h');
Read more about DateTime class
echo date('d/m/Y G', strtotime('-5 hour')) . 'h';
Have a look to strftime() :
<?
$time=time(); // This is the time in second from the epoch
$time -= 3600 * 5; // I subtract 5 hour
echo strftime("%d/%m/%Y %H", $time); // I print the calculated time
?>
Is there a nice simple shorthand way of finding out how many seconds past midnight a certain datetime is? Not how many seconds it is away from now, the seconds past in that day
eg:
2015-04-10 00:00:00 should returns 0
2015-04-12 09:20:00 should returns 33600
2015-04-14 15:20:00 should returns 55200
Is there a nice short method of doing this?
Here is a simple code for you.
Any day code
<?php
$date = "2015-04-12 09:20:00";
$midnight = strtotime(date("Y-m-d 00:00:00", strtotime($date)));
$now = strtotime($date);
$diff = $now - $midnight;
echo $diff;
?>
Current day code
<?php
$midnight = strtotime("midnight");
$now = date('U');
$diff = $now - $midnight;
echo $diff;
?>
Maybe a more clean code would be to use strtotime("midnight", <EpochTime>);
<?php
$date = "2015-04-12 09:20:00";
$midnight = strtotime("midnight", strtotime($date));
$now = strtotime($date);
$diff = $now - $midnight;
echo $diff;
?>
A day is 24 hours is 60 minutes is 60 seconds is 1000 miliseconds, so timestamp % (24*60*60*1000) could work, maybe, at least if you are on the same time zone as the epoch php is using (or whatever you timestamp is coming from). It would work at least for nothing too demanding of accuracy. PHP uses your system's time, so many things will break the idea (change the clock, DST, leap seconds, etc)
I would personally use timestamp - startofday timestamp and calculate using the language's calendar instead.
I have a PHP DateTime variable.
How can I reduce or subtract 12hours and 30 minutes from this date in at PHP runtime?
Subtract 12 Hours and 30 minutes from a DateTime in PHP:
$date = new DateTime();
$tosub = new DateInterval('PT12H30M');
$date->sub($tosub);
The P stands for Period. The T stands for Timespan.
See DateTime, DateTime::sub, and DateInterval in the PHP manual. You'll have to set the DateTime to the appropriate date and time, of course.
Try with:
$date = new DateTime('Sat, 30 Apr 2011 05:00:00 -0400');
echo $date->format('Y-m-d H:i:s') . "\n";
$date->sub(new DateInterval('PT12H30M'));
echo $date->format('Y-m-d H:i:s') . "\n";
//Result
2011-04-30 05:00:00
2011-04-29 16:30:00
Try strtotime() function:
$source_timestamp=strtotime("Sat, 30 Apr 2011 05:00:00 -0400");
$new_timestamp=strtotime("-12 hour 30 minute", $source_timestamp);
print date('r', $new_timestamp);
Maybe it will be useful for some cases
$date = new DateTime();
$date->modify('-12 hours -30 minutes');
echo $date->format('H:i:s');
try using this instead
//set timezone
date_default_timezone_set('GMT');
//set an date and time to work with
$start = '2014-06-01 14:00:00';
//display the converted time
echo date('Y-m-d H:i',strtotime('+1 hour +20 minutes',strtotime($start)));
If you are not so familiar with the spec of DateInterval like PT12H30M you can proceed with more human readable way using DateInterval::createFromDateString as follows :
$date = new DateTime();
$interval = DateInterval::createFromDateString('12 hour 30 minute');
$date->sub($interval);
Or with direct interval in sub function like below :
$date = new DateTime();
$date->sub(DateInterval::createFromDateString('12 hour 30 minute'));
Store it in a DateTime object and then use the DateTime::sub method to subtract the timespan.
I used in one line, for 12 hours only, and just as an hour display
$date = new DateTime(); $date->modify('-12 hours'); echo $date->format('H')-0;
I used the -0 since sometimes it put a 0 in front of the digit unless I done that, strange.
Here is detailed description of date function,
Using simply strtotime
echo date("Y-m-d H:i:s",strtotime("-12 hour -30 minutes"));
Using DateTime class
$date = new DateTime("-12 hour -30 minutes");
echo $date->format("Y-m-d H:i:s");
I need to calculate the timestamp of exactly 7 days ago using PHP, so if it's currently March 25th at 7:30pm, it would return the timestamp for March 18th at 7:30pm.
Should I just subtract 604800 seconds from the current timestamp, or is there a better method?
strtotime("-1 week")
strtotime is your friend
echo strtotime("-1 week");
http://php.net/strtotime
echo strtotime("-1 week");
There is the following example on PHP.net
<?php
$nextWeek = time() + (7 * 24 * 60 * 60);
// 7 days; 24 hours; 60 mins; 60secs
echo 'Now: '. date('Y-m-d') ."\n";
echo 'Next Week: '. date('Y-m-d', $nextWeek) ."\n";
// or using strtotime():
echo 'Next Week: '. date('Y-m-d', strtotime('+1 week')) ."\n";
?>
Changing + to - on the first (or last) line will get what you want.
From PHP 5.2 you can use DateTime:
$timestring="2015-03-25";
$datetime=new DateTime($timestring);
$datetime->modify('-7 day');
echo $datetime->format("Y-m-d"); //2015-03-18
Instead of creating DateTime with string, you can setTimestamp directly on object:
$timestamp=1427241600;//2015-03-25
$datetime=new DateTime();
$datetime->setTimestamp($timestamp);
$datetime->modify('-7 day');
echo $datetime->format("Y-m-d"); //2015-03-18
<?php
$before_seven_day = $date_timestamp - (7 * 24 * 60 * 60)
// $date_timestamp is the date from where you found to find out the timestamp.
?>
you can also use the string to time function for converting the date to timestamp. like
strtotime(23-09-2013);