2

I've run into a strange problem that's at the cause of a bigger issue down the road. I'll define the initial problem (that's not really a problem) followed by the issue caused by it.

I have a javascript function that defines a date variable and sends it as data in a post request to a php function:

var currentdate = new Date();

var showDailyEvents = function() {
    $.post(
        '/php/returnEvents.php',
        {
            myDate: currentdate,
            period: "daily"
        },
        "html"
    );
};

showDailyEvents();

When I run this on a Mac (across all browsers), currentdate is "Fri Mar 17 2017 15:37:29 GMT-0700 (PDT)". However when I run it on a PC (Windows 7 specifically), currentdate is "Fri Mar 17 2017 15:37:29 GMT-0700 (Pacific Daylight Time)". When this variable is passed into the php function, I try to convert it to a timestamp like so:

$startDay = strtotime($_POST['myDate']);

But when I check that variable, in Mac it is 1489790249 while on PC it is returned as false. The only difference between the variables on the two computers is the PDT vs Pacific Daylight Time.

Has anyone encountered this issue? Or have any idea how to solve this appropriately?

EDIT: Please note, I need the 'Fri' part of the date

ConorBaumgart
  • 493
  • 1
  • 3
  • 18
  • 1
    Why not let the client send you the timestamp? http://stackoverflow.com/questions/221294/how-do-you-get-a-timestamp-in-javascript – Webbanditten Mar 17 '17 at 23:09
  • @Webbanditten I could be misunderstanding, but isn't that why I'm doing? The new Date() object is being created on the client's browser and then sent in the request. – ConorBaumgart Mar 17 '17 at 23:12
  • No you are parsing in the full date as a string with time zone information into a PHP method that then converts the text to Unix timestamp. http://php.net/manual/en/function.strtotime.php I'm thinking that the PHP method don't like the Windows string, therefore my thoughts are to convert the date to Unix timestamp and then send that to the server. – Webbanditten Mar 17 '17 at 23:16
  • 1
    Suggestion: Use `var currentdate = Date.now();` You will get a unix epoch timestamp (on all systems). You do not neeed the `Fri` as you can output the date in the presentation layer in ANY format you like as long as you have a usable date – RiggsFolly Mar 17 '17 at 23:23

1 Answers1

0

Should you insist on passing the date/time string, DateTime with explicit format could be an option:

<?php

$arr = array(
    'Fri Mar 17 2017 15:37:29 GMT-0700 (PDT)',
    'Fri Mar 17 2017 15:37:29 GMT-0700 (Pacific Daylight Time)'
);

foreach($arr as $val){
    $d = DateTime::createFromFormat('D M d Y H:i:s O+', $val);
    if($d !== false){
        echo $val . ": " . $d->getTimeStamp() . "\n";
    }
}
?>

This generates:

Fri Mar 17 2017 15:37:29 GMT-0700 (PDT): 1489790249
Fri Mar 17 2017 15:37:29 GMT-0700 (Pacific Daylight Time): 1489790249

Here, + in the format specification means that the rest of the input is skipped so whatever comes after the timezone specification (O) is effectively ignored.

ewcz
  • 12,819
  • 1
  • 25
  • 47