Skip to content Skip to sidebar Skip to footer

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

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 timestampvar now = newDate(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("<?phpecho$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:

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

Post a Comment for "Convert Php Date Into Javascript Date Format"