Code _php tips in PHP based on the date and day of the year

Source: Internet
Author: User
This index value in addition to the instant Noodles data recording and searching, but also plays the role of date information, the amount of information is considerable.
So how do you restore the index value to date information that is available?
Date (' Z ') returns the day ordinal of the year, and the return value is an integer starting with 0 to 365. To restore these integers to date information, we only need a simple calculation. For example, on the 159th day of this year (2008) (actually 160 days, starting from 0 in PHP, 0 corresponds to the first day of the regular), the date and week method for the calculation is as follows:
Copy Code code as follows:

<?php
$MyDate = 159; 159th Day
$milliseconds = Mktime (0,0,0,1,1,2008) + $MyDate * 86400; Get Unix time stamp
$msg = Date (' F JS Y, l ', $milliseconds); Format Date Output
Echo $msg;
?>

Run the above program, the information displayed on the browser will be: June 8th 2008, Sunday
Explanation:
A. $milliseconds is the UNIX time stamp variable, which indicates the number of milliseconds experienced on the 159th day of 2008 from the Unix Age (January 1, 1700), which is used as a parameter in the date () function to participate in the calculation dates information, which is an important basis. To correctly calculate the value of $milliseconds, we first need to get the number of milliseconds that we experienced in 2008 1st 1st, that is, Mktime (0,0,0,1,1,2008), then add a 159-day number of milliseconds, that is, $MyDate * 86400, because, 86400 milliseconds per day.
Two. $msg is the date information of our formatted output, the value of this variable is obtained through the date () function, the first parameter "' F JS Y, L '" is only formatted, and can be set to the other as needed, and the second parameter is the number of milliseconds (Long integer type), The meaning is the total number of milliseconds that have elapsed since the start of UNIX years to a particular point in time.
In order to verify the correctness of the program, we can print the number of milliseconds experienced June 8, 2008: Mktime (0,0,0,6,8,2008), its value will be the same as $milliseconds: 1212854400.

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.