Convert PHP date into javascript date format

I have a PHP script that outputs an array of data. This is then transformed into JSON using the json_encode() function.

My issue is I have a date within my array and it’s not in the correct JavaScript format. How can I convert this within PHP so it is?

$newticket['ThreadID'] =  $addticket;
$newticket['Subject'] =  $subject;
//$newticket['DateCreated'] =  date('d-m-Y G:H');

Instead of the above for the date I need the equivalent of the JavaScript function

new Date()

When I output the above I get the following “Fri Jun 01 2012 11:08:48 GMT+0100 (GMT Daylight Time)” However, If I format my PHP date to be the same, then JavaScript rejects it. Confused…

Can anyone help?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

You should probably just use a timestamp

$newticket['DateCreated'] = strtotime('now');

Then convert it to a Javascript date

// make sure to convert from unix timestamp
var now = new Date(dateFromPHP * 1000);

Solution 2

Javascript Date class supports ISO 8601 date format so I would recommend:

      date('c', $yourDateTime); 
      // or for objects

documentation says that:
format character ‘c’ is ISO 8601 date (added in PHP 5)
example: 2004-02-12T15:19:21+00:00

for more information:

Solution 3

It is pretty simple.

PHP code:

$formatted_date = $newticket['DateCreated'] =  date('Y/m/d H:i:s');

Javascript code:

var javascript_date = new Date("<?php echo $formatted_date; ?>");

Solution 4

If you want to be more precise with your timestamp, you should use microtime() instead of now().

That gives you:

echo round(microtime(TRUE)*1000);

For a milisecond, javascript-like timestamp in php.

Solution 5

Is very simple, I’m use this:

new Date("<?= date('Y/m/d H:i:s'); ?>");

Solution 6

An improvement or simplification of @jeremyharris answer would be this one:

DateTime objects in PHP have the getTimestamp() format, use it and multiply the value by 1000:

    $phpDateTimeStamp = new Date('Y/m/d H:i:s')->getTimestamp() * 1000; 

// JavaScript
let startTime = new Date(phpDateTimeStamp);

Solution 7

$newticket['DateCreated'] = date('d-m-Y G:H', strtotime($phpDateVariable));

Solution 8

The most robust way is to extend your date object to parse strings as objects that your application can use. A quick example of parsing the mysql datetime string

 let months = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
 let date = str.split(" ")[0];
 let time = str.split(" ")[1];

 let [Y, M, d] = [date.split("-")[0], date.split("-")[1], date.split("-")[2]];
 let [H, m, s] = [time.split(":")[0], time.split(":")[1], time.split(":")[2]];

 return {
  FullYear: Y,
  Month: M - 1,
  MonthString: months[M - 1],
  Date: d,
  Time: {Hour: H, Minute: m, Second: s},

If you are inclined you may include a second parameter that describes the type of string being passed with a default value, and do a string test to determine if it is a unix timestamp or a javascript timestamp or a string, and then use the returned object to parse your date and times, this is a more rounded solution because it will allow you to build an interface that dynamically handles multiple date specifiers.

Solution 9

 var phpTimeStamp = '2022-03-20 12:20:20'; /* timestamp format*/

 var jsDateObject = new Date(Date.parse( phpTimeStamp ));

Now you can access all property of a date object like

 jsDateObject.getHours(); //  gives hour 

And other functions as well.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply