Convert PHP date into javascript date format

Related searches

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?


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);

Javascript: convert PHP date format 'Y-m-d H:i:s' to a valid javascript , my_date = my_date.replace(/-/g, "/"); alert(my_date);. 3. var d = new Date( my_date);. 4. document.getElementById("demo").innerHTML = d;. 5. . CSS Tidy. The date_format() function returns a date formatted according to the specified format. Note: This function does not use locales (all output is in English). Tip: Also look at the date() function, which formats a local date/time.


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

Passing a full Date Time string from PHP to Javascript: beware of , format ISO 8601 accepted by all browsers// PHPdate(“c” to easily convert it to ISO 8601 in php you can use the date() function passing “c” as� These are the built-in functions of PHP. The strtotime() first converts the date into the seconds, and then date() function is used to reconstruct the date in any format. Below some examples are given to convert the date format. Change YYYY-MM-DD to DD-MM-YYYY. In the below example, we have date 2019-09-15 in YYYY-MM-DD format, and we will


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; ?>");

PHP date_format() Function, The date_format() function returns a date formatted according to the specified format. Note: This function does not use locales (all output is in English). Tip: Also � Date Input - Parsing Dates. If you have a valid date string, you can use the Date.parse() method to convert it to milliseconds. Date.parse() returns the number of milliseconds between the date and January 1, 1970:


Is very simple, I'm use this:

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

JavaScript Date parse() Method, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java� This is a short PHP guide on how to convert a regular date string into a Unix timestamp. As you probably already know, “Unix Time” is the number of seconds that have passed since the 1st of January, 1970. For the sake of this example, let’s say that we want to convert 2019-04-01 10:32:00 into an Epoch timestamp. strtotime


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:

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

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

The best string format for string parsing is the date ISO format together with the JavaScript Date object constructor. Examples of ISO format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS. But wait! Just using the "ISO format" doesn't work reliably by itself. String are sometimes parsed as UTC and sometimes as localtime (based on browser vendor and version).


2.Our second option is to let MySQL do the work. MySQL has functions we can use to convert the data at the point where we access the database. UNIX_TIMESTAMP will convert from DATETIME to PHP timestamp and FROM_UNIXTIME will convert from PHP timestamp to DATETIME. The methods are used within the SQL query.


First you can give JavaScript's Date object (class) the new method 'fromYMD()' for converting MySQL's YMD date format into JavaScript format by splitting YMD format into components and using these date components:


The combinations of the year, the month, the day and the separators can be endless. The following are 100 common date text formats and the formulas to convert them into the real dates. After you convert the text into the date format, you can use the format cells method again to transfer to the format you want.