Search code examples
phpdateposix

PHP Date and POSIX date


I have two dates (from a datatime picker) that I send using post in PHP to this same page (I use a hidden <input> in a form).

$(function(){
    flatpickr('#calendar',{
        mode: "range",
        onChange: function(dates) {
            if(dates.length>1){
                $("#formdate .from").val(+dates[0]/1000);
                $("#formdate .to").val((+dates[1])/1000);
                $("#formdate").submit(); 
             }
        }
    });
});

Those dates are POSIX date, for example 1490569200 is 2017-03-27 00:00:00.

You can see that I divide them by 1000; it's because I use them in MySQL so it needs to be in second and not in millisecond.

Here is my datetime picker :

enter image description here

When I valid this second date (here the 26th), it call the function above and refresh the page. If I do a echo of my POSIX date I got the following :

1488326400, 1490486400 that are respectively the 1st and the 26th of March (normal behavior). Now I use the following function in PHP to convert them in a readable format :

echo date('l jS \of F Y',$from);
echo date('l jS \of F Y',$to);

It displays me the following :

Wednesday 1st of March 2017
Sunday 26th of March 2017

So until here, everything works fine !

But now if I pick up a date equal or above the 27th of March, here it's what's happen :

enter image description here

I do an echo of my POSIX dates : 1490313600, 1490655600 that are respectively the 24th and 28th (so no problem).

But when I use PHP date for the readable format :

echo date('l jS \of F Y',$from);
echo date('l jS \of F Y',$to);

Friday 24th of March 2017
Monday 27th of March 2017

Indeed, every dates I pick equal or above the 27th will be displayed as the day before.

If I use echo date('l jS \of F Y',1490655600), same issue, it will give me the 27th and no the 28th.

What's wrong ?


Solution

  • As said in the comment, that was due to the winter/summer time.