This article describes how to implement the strtotime () and date () functions in php in js imitation. it involves related techniques related to javascript time operations and is of great practical value, for more information about how to use js to simulate strtotime () and date () functions in php, see the following example. Share it with you for your reference. The details are as follows:
In js, there are no strtotime () and date () functions like php. you can directly convert the timestamp. next we will define a function to implement specific timestamp conversion functions in js.
Function datetime_to_unix (datetime) {var tmp_datetime = datetime. replace (/:/g, '-'); tmp_datetime = tmp_datetime.replace (// g, '-'); var arr = tmp_datetime.split ("-"); var now = new Date (Date. UTC (arr [0], arr [1]-1, arr [2], arr [3]-8, arr [4], arr [5]); return parseInt (now. getTime ()/1000);} function unix_to_datetime (unix) {var now = new Date (parseInt (unix) * 1000); return now. toLocaleString (). replace (/year | month/g ,"-"). replace (/day/g, "");} var datetime = '2017-11-16 10:36:50 '; var unix = datetime_to_unix (datetime); document. write (datetime + 'converted timestamp: '+ unix + ''); var unix = 1353033300; var datetime = unix_to_datetime (unix); document. write (unix + 'converted date: '+ datetime );
If you want to pop up in the format of 10:00:00, you can do it.
Script function getLocalTime (nS) {return new Date (parseInt (nS) * 1000 ). toLocaleString (). replace (/year | month/g ,"-"). replace (/day/g, "");} alert (getLocalTime (1177824835); script
Complete instance