The code in PHP that calculates the date year month and day based on the date of a year

  • 2020-03-31 21:36:35
  • OfStack

In addition to the instant noodles data record and search, this index value also plays the role of recording date information, the amount of information is considerable.
So how do you restore the date information whose index value is available?
Date ('z') returns the day of the year with an integer starting from 0 to 365. To restore these integers to date information, we need only a simple calculation. Take the 159th day of this year (2008) (actually 160 days, PHP starts with 0, 0 corresponds to the normal first day) as an example, and calculate its date and week as follows:
 
<?php 
$MyDate=159; //The 159th day
$milliseconds = mktime(0,0,0,1,1,2008) + $MyDate * 86400; //Gets a UNIX time stamp
$msg = date('F jS Y , l',$milliseconds); //Format date output
echo $msg; 
?> 

Running the above program, the information displayed on the browser will be: June 8th 2008, Sunday
Explanation:
$milliseconds is the UNIX timestamp variable that indicates the number of milliseconds since the UNIX epoch (January 1, 1700) on day 159 of 2008, and it will be used as an argument to participate in calculating date information in the date() function. To calculate the value of $milliseconds correctly, we first need to get the number of milliseconds experienced on day 1,2008, i.e., mktime(0,0,0,1,1,2008), and then add the number of milliseconds in 159 days, i.e., $MyDate * 86400, because, for each day, 86400 milliseconds.
2. $MSG is our date formatted output information, the variable values obtained by the date () function, the first parameter "' jS Y F, l '" just formatting format, can also according to need to set up for the other, the second parameter is the number of milliseconds (long), its meaning is from UNIX starting to total number of milliseconds experienced a particular moment.
To verify the correctness of the program, we can print the number of milliseconds experienced on June 8,2008: mktime(0,0,0,6,8,2008), which will have the same value as $milliseconds: 1212854400.

Related articles: