PHP Date 4 Weeks Ago - php

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 :)

Related

PHP convert the day date with the time

I am working on my php script to set up the date with the time. I need some help with convert the day date to the current day and next day date, example: my current time is 15:27 and my current date is 27-11-2019 so when I have the string for the variable get_time1 is 06:00:00, I want to convert it to 28-11-2019 06:00:00. When I have the variable get_time2 that have the time which it is 23:00:00 as my current time is before 23:00:00 so i want to convert the date with the current date with the time to 27-11-2019 23:00:00.
Code:
<?php
$get_time1 = '06:00:00';
$get_time2 = '23:00:00';
date_default_timezone_set('Europe/London');
$Date = date('Y-m-d');
$time = date('H:i:s');
?>
Can you please show me an example how I can set up the day date with the time 06:00:00 and 23:00:00 as if the time 06:00:00 is after 12am to set up the next day date and if the time 23:00:00 is before 12am then set up the time with the current date?
Thank you.
This just creates a DateTime object from the time (which will default it to todays date) and if this is less than the current date and time, it adds 1 day...
$date = DateTime::createFromFormat("H:i:s", $get_time2);
if ( $date < new DateTime() ) {
$date->modify("+1 day");
}
which gives
2019-11-27 23:00:00
and for $get_time1...
2019-11-28 06:00:00
If you use a DateTime that will allow you to do date arithmetic.
$now = new DateTime();
$tomorrow = $now->modify("+1 day");
You can also use strtotime to get a unix timestamp as explained in this answer.
$tomorrow = strtotime('+1 day');
maybe this will do?
$offset = timezone_offset_get( timezone_open( "Europe/London" ), new \DateTime() );
echo 'in London' . gmdate('d-m-Y H:i:s', date( "U" )+$offset);
echo 'current location: ' . date('d-m-Y H:i:s', date( "U" ));

How will i get millisecond date now vs date tommorow

i have this date and time i just want to get the millisecond until every 2:00 am.
example date/time: 10-18-2017 00:00:00 -- 2 hours before 2 am the millisecond before 2:00 am is 7200000.
what should i do or what method should i use. Thanks in advance
$datetimenow = date("m-d-Y H:i:s", strtotime('+0 hours'));
$TwoAMDay = time();
// add one day if next 2am is tomorrow
if(date('H', $TwoAMDay) >= 2) {
$TwoAMDay = $TwoAMDay + 86400;
}
$twoAMDate = date('Y-m-d 02:00:00', $TwoAMDay);
$twoAMTime = strtotime($twoAMDate);
$differenceMilliseconds = (1000 * $twoAMTime) - round(microtime(true) * 1000);

how to get date after 2 days from the given time in php

i want to find out the date after days from the given time.
for example. we have date 29 may 2015
and i want to cqlculate the date after 2 days of 25 may 2015
$Timestamp = 1432857600 (unix time of 29-05-2015)
i have tried to do it with following code but it is not working
$TotalTimeStamp = strtotime('2 days', $TimeStamp);
Missed the + - strtotime('2 days', $TimeStamp); .
Add the + to + 2 days.
Use date & strtotime for this - You can try this -
echo date('d-m-Y',strtotime(' + 2 day', strtotime('2015-05-16')));
$Timestamp & $TimeStamp are not same(may be typo). For your code -
$Timestamp = strtotime(date('Y-m-d'));
$TotalTimeStamp = strtotime('+ 2 days', $Timestamp);
echo date('d-m-Y', $TotalTimeStamp);
Php does have a pretty OOP Api to deal with date and time.
This will create a \DateTime instance using as reference the 25 May 2015 and then you can call the modify method on that instance to add 2 days.
$date = new \DateTime('2015-05-25');
$date->modify('+2 day');
echo $date->format('Y-m-d');
You may find this resource useful:
http://code.tutsplus.com/tutorials/dates-and-time-the-oop-way--net-35395
You can also just add seconds to your timestamp if you have a timestamp ready:
$NewDateStamp = $Timestamp + (60*60*24 * 2);
In the above, sec * min * hours = day -- or 86400 seconds. * 2 = 2 days.
In PHP 5 you can also use D
<?php
$date = date_create('2015-05-16');
date_add($date, date_interval_create_from_date_string('2 days'));
echo date_format($date, 'Y-m-d');
?>
OR
<?php
$date = new DateTime('2015-05-16');
$date->add(new DateInterval('2 days'));
echo $date->format('Y-m-d') . "\n";
?>

PHP, need to subtract 12 hours and 30 minutes from a DateTime

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");

How do I add 24 hours to a unix timestamp in php?

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)

Categories