JS converts the time stamp generated by PHP into a date in a specific date format

Source: Internet
Author: User

Requirements:Converts the time of the int type displayed on the first page to the time format of the date type:when Questionmodel gets to question list data, including question[' Pub_time '), it needs to be converted to a specific time format for display when it is displayed. when inserting a problem record, the Pub_time field uses the PHP time () function, gets the current timestamp integer, and then inserts it into the MySQL data table, so its format is int type.  

However, when displayed in the HTML of view view, it needs to be formatted as a time format for display. But because each problem in order to facilitate pagination display data is through the JS splicing, so the function of conversion depends on JS.  

search on the internet about JS to convert int to date format data is copied, and is not practical, so after reading the official PHP and JS Manual, learned that:
    1. Time () in PHP is the timestamp of the current UNIX, accurate to the number of seconds https://secure.php.net/manual/zh/function.time.php
                 
    1. The new date () in JS can convert the int data to date type data by passing parameters to date, and the parameters are accurate to the number of milliseconds https://developer.mozilla.org/zh-CN/ Docs/web/javascript/reference/global_objects/date
  Now the problem is very simple to convert, through the JS Date object to the corresponding month and day can be, by default, the value of the date variable is a time format string.  
var  Time // 1483970035 var Date New Date (time *1000); var Date. getFullYear (); var date. GetMonth () + 1; var Date. getDate  = year + '-' + month + '-' + day;

 

The result of the implementation:

Really just the difference between the number of seconds and the number of milliseconds, after ignoring the confusion for a long time.

JS converts the time stamp generated by PHP into a date in a specific date format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.