-1

Dynamically generating select in a form which works fine for dates less than March 31 but for April 1st and afterwords it is wrong. You can see I am specifying GMT exclusively which worked perfectly at-least for date:March 31.

    $today = strtotime("today GMT");
    <select name="date">
    <option value=<?php echo $d = strtotime('0 day',$today); ?>> <?php echo date('d M, Y', $d).'-'.$d; ?></option>
    <option value=<?php echo $d = strtotime('1 day',$today); ?>> <?php echo date('d M, Y', $d).'-'.$d; ?></option>
    <option value=<?php echo $d = strtotime('2 day',$today); ?>> <?php echo date('d M, Y', $d).'-'.$d; ?></option>
<-remainings->
</select>

generated code

28 Mar, 2019-1553731200<--Correct March 28, 2019 12:00:00 AM
29 Mar, 2019-1553817600<--Correct
30 Mar, 2019-1553904000<--Correct
31 Mar, 2019-1553990400<--Correct
01 Apr, 2019-1554073200<--Wrong March 31, 2019 11:00:00 PM  (this and remainings should be April <nextday>, 2019 12:00:00 AM)
02 Apr, 2019-1554159600<--Wrong April 1, 2019 11:00:00 PM 
03 Apr, 2019-1554246000<--Wrong April 2, 2019 11:00:00 PM
04 Apr, 2019-1554332400<--Wrong April 3, 2019 11:00:00 PM
05 Apr, 2019-1554418800<--Wrong April 4, 2019 11:00:00 PM
06 Apr, 2019-1554505200<--Wrong April 5, 2019 11:00:00 PM 
kiranking
  • 306
  • 11
  • 29
  • 1
    [GMT went on Daylight Saving Time](https://greenwichmeantime.com/daylight-saving-time/) on 31-March at 1 am. Sorry, I don't know what you need to do about it. Perhaps use UTC instead? – DinoCoderSaurus Mar 31 '19 at 18:03
  • Everyday Json file will be generated on submission of the form with file name like `.json`. Later this file will be accessed by mobile app and same method is implemented in mobile app to dynamically generate filename before hitting the url. Because of this issue filename is not same as app is expecting just for > Apr-01. – kiranking Mar 31 '19 at 20:04

1 Answers1

1

Try changing this $today = strtotime("today GMT"); to this
$today = strtotime("today",gmdate('U'));

When I executed this $today = strtotime("today GMT"); on my system (31-Mar-2019 18:00 time zone EDT), the result was 1553990400 30 Mar, 2019 20:00

I read the PHP: Datetime Relative Formats Doc and could find no indication that time zone is used in any format, which is why I tried it with gmdate('U').

This code:

echo "\ngmdate\n";
echo "current date: ",strtotime("today"),"<-- ",date('d M, Y H:i'),"\n";
echo "'today GMT': ",strtotime("today GMT"),"<--",date('d M, Y H:i',strtotime("today GMT")),"\n\n";
$todayGMdate = strtotime("today",gmdate('U'));

echo $todayGMdate,"<-- ",date('d M, Y H:i',$todayGMdate),"\n";
for ($i = 0; $i < 10; $i++) {
    $d=strtotime("+$i day",$todayGMdate);
    echo date('d M, Y', $d).'-'.$d," <-- ",date('d M, Y H:i',$d),"\n";
}

Produces this result:

gmdate
current date: 1554091200<-- 01 Apr, 2019 10:44
'today GMT': 1554076800<--31 Mar, 2019 20:00

1554091200<-- 01 Apr, 2019 00:00
01 Apr, 2019-1554091200 <-- 01 Apr, 2019 00:00
02 Apr, 2019-1554177600 <-- 02 Apr, 2019 00:00
03 Apr, 2019-1554264000 <-- 03 Apr, 2019 00:00
04 Apr, 2019-1554350400 <-- 04 Apr, 2019 00:00
05 Apr, 2019-1554436800 <-- 05 Apr, 2019 00:00
06 Apr, 2019-1554523200 <-- 06 Apr, 2019 00:00
07 Apr, 2019-1554609600 <-- 07 Apr, 2019 00:00
08 Apr, 2019-1554696000 <-- 08 Apr, 2019 00:00
09 Apr, 2019-1554782400 <-- 09 Apr, 2019 00:00
10 Apr, 2019-1554868800 <-- 10 Apr, 2019 00:00

It seems like strtotime("today GMT") gets start of today in current locale, and then adds the gmt offset.

I suspect this note from the doc is in play:

Note:

Relative statements are always processed after non-relative statements. This makes "+1 week july 2008" and "july 2008 +1 week" equivalent.

Exceptions to this rule are: "yesterday", "midnight", "today", "noon" and "tomorrow". Note that "tomorrow 11:00" and "11:00 tomorrow" are different. Considering today's date of "July 23rd, 2008" the first one produces "2008-07-24 11:00" where as the second one produces "2008-07-24 00:00". The reason for this is that those five statements directly influence the current time.

DinoCoderSaurus
  • 6,110
  • 2
  • 10
  • 15
  • AFAIU GMT should produce same timestamp regardless of the device locale. Correct me if I am wrong here? But per your code above timestamp is not exact 12:00 AM which is what I am after because when I say each day file pre-genarated then that file will be accessible from 12:00am to 11:59:59 PM. When I checked the timestamp of above output @ epochconverter.com its +4 ahead of GMT. I am totally confused whether our code is fault or php or TIME itself :) – kiranking Apr 01 '19 at 14:02
  • So sorry, the example was flawed. I have added clarification to the answer. – DinoCoderSaurus Apr 01 '19 at 14:50