Convert PHP date into javascript date format
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:
<?php
date('c', $yourDateTime);
// or for objects
$dateTimeObject->format('c');
?>
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: http://php.net/manual/en/function.date.php
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
Is very simple, I'm use this:
new Date("<?= date('Y/m/d H:i:s'); ?>");
Solution 5
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.
Related videos on Youtube
LeeTee
Updated on July 09, 2022Comments
-
LeeTee almost 2 years
I have a PHP script that outputs an array of data. This is then transformed into
JSON
using thejson_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?
-
LeeTee almost 12 yearsthe javascript date format is: Fri Jun 01 2012 11:08:48 GMT+0100 (GMT Daylight Time) not a timestamp
-
LeeTee almost 12 yearsthe $phpDateVariable is already in the format you state above. I need to put into javascript format which is Fri Jun 01 2012 11:08:48 GMT+0100 (GMT Daylight Time)
-
Scott Saunders almost 12 yearsAre you just asking for how to format a date in PHP? Go look at the formatting options in the documentation: us.php.net/manual/en/function.date.php
-
Scott Saunders almost 12 yearsNo, there are multiple ways of instantiating a date object, including a timestamp: w3schools.com/jsref/jsref_obj_date.asp
-
jeremyharris almost 12 years@LeeTee The easiest and most transportable format is a timestamp, which you can then convert to whatever you want.
-
contrid almost 6 yearsThis works very well with Javascript
new Date();
good answer! -
belst almost 6 yearsEcmascript does not support full ISO 8601. only a simplified version of it
-
Mikepote over 5 years@RichardSocker
<?=
is short for<?php echo ...
-
Batsheva Hansav over 5 yearsThanks man, new thing to me, actually I tried it but I got 500 error maybe cause another error. :)
-
Theva over 3 years// in JavaScript we have to cast the php timestamp to int
let startTime = new Date(parseInt(phpDateTimeStamp));