1

I'm fetching dates from MySQL as strings (Y-m-d format). I need to display a chart using Highcharts. Highcharts uses javascript Date.UTC function:

Return the number of milliseconds between a specified date and midnight January 1 1970.

data: [
    [Date.UTC(1970,  9, 27), 0   ],
    [Date.UTC(1970, 10, 10), 0.6 ],
    [Date.UTC(1970, 10, 18), 0.7 ],
    [Date.UTC(1970, 11,  2), 0.8 ],
    [Date.UTC(1970, 11,  9), 0.6 ],

But i'd like to avoid javascript and do in in PHP (assigning a JSON object - the chart - to the page itself). What's the equivalent of Date.UTC function in PHP (regardless the server datezone)?

$date      = '2012-07-07';
$millisecs = 1000 * unix_timestap_utc_regardless_server_zone($date);
1
  • From PHP to JS, put this in a variable: date('Y, n-1, j-1', strtotime($date)); and echo out Date.UTC($var) Commented Mar 26, 2020 at 21:26

3 Answers 3

2

I would use

date_default_timezone_set('UTC');    
$today = date(getdate());

That would set $today as the date.

EDIT: Niclas is also right, this is just how I would do it.

EDIT 2: You can replace getdate() with a valid timestamp in PHP, if you like...

EDIT 3: Sorry... Misunderstanding... use strtotime() to make a valid timestamp!

Sign up to request clarification or add additional context in comments.

Comments

1

You can look at this:

http://php.net/manual/en/function.mktime.php

http://php.net/manual/en/function.strtotime.php

http://php.net/manual/en/function.date.php

2 Comments

Thanks. But (for example) strtotime is going to use the time zone specified in my PHP.ini, right?
strtotime is only going to use defult timezone if you dosn't provide one your self, like strtotime("00:00:00 UTC")
0

Obtain the PHP Timestamp and then convert into Javascript Timestamp with a multiplication

$phpTimestamp = $dateObject->getTimestamp();
$javascripTimestamp = $phpTimestamp * 1000;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.