PHP date, time-related knowledge collation

Source: Internet
Author: User

Unix Timestamp: The number of seconds elapsed from January 1, 1970 0 o'clock to the current time.

1. Convert a date to a Unix timestamp
Mktime (): can automatically correct out-of-bounds input

Prototype: Mktime (hour,minute,second,month,day,year);
Instance:

<?php    echo Date ("Y-m-d", Mktime (0,0,0,2,12,2015));   2015-12-12    echo Date ("Y-m-d h:i:s", Mktime (7,50,59,10,21,2015)),//2015-10-21 07:50:59    echo Date ("y-m-d", Mktime (0,0,0,12,33,2015));   2016-01-02, date more than 31 days, automatic correction time?>

2. Parse the date time of the English text into a UNIX timestamp
Strtotime ()
Prototype: Strtotime (Time,now)

<?php    Echo strtotime ("Now");   1447987573    echo Date ("Y-m-d", Strtotime ("Now");   2015-11-20    echo Date ("Y-m-d h:i:s", Strtotime ("Now");   2015-11-20 02:49:18    echo Date ("Y-m-d", Strtotime ("8 December"));   2015-12-08?>

Example: Writing a countdown program for anniversaries through Strtotime ()

<?php            $now = Strtotime ("Now");  Start time $endtime = Strtotime (' 2018-10-12 10:20:59 ');  Graduation time, turn into timestamp $second = $endTime-$now;   Gets the timestamp from graduation to present (number of seconds) $year = Floor ($second/3600/24/365);  The number of years calculated from this timestamp the floor   () function is rounded down to the nearest integer. $temp = $second-$year *365*24*3600;  Remove the number of seconds from the timestamp for the remainder of the month $month = Floor ($temp/3600/24/30);  Conversion Moon Number $temp = $temp-$month *30*24*3600;  Minus the number of seconds in the entire month, the number of seconds left for the number of days = Floor ($temp/3600/24);  Convert the remaining days $temp = $temp-$day *24*3600;  Minus the number of seconds in the day, the number of seconds left $hour = Floor ($temp/3600);  The number of hours remaining $temp = $temp-$hour *3600;  Minus the number of seconds in the whole hour, the number of seconds left $minute = Floor ($temp/60);  Calculate the remaining fraction $second1 = $temp-$minute *60;   Number of seconds left echo "distance graduated {$year} years {$month} month {$day} days {$hour} hours {$minute} minutes {$second 1} seconds";  2 years from graduation October 27 days 1 hours 56 minutes 14 seconds?>

Extension: Calculation of dates
The simplest way to calculate the distance between two dates is by calculating the two Unix timestamp only.
Example: To calculate the age of a user by receiving a birth date from an HTML form submission in a PHP script

<?php        $year = 1989; $month = n; $day =; $birthday = Mktime (0,0,0, $month, $day, $year);   Convert to Timestamp $nowday = Strtotime ("Now");  Gets the current datetime timestamp $ageunix = $nowDay-$birthday; $age = Floor ($ageUnix/(60*60*24*365)); echo "Age: $age";   Age:25?>

Date and time formatted output
The UNIX timestamp is used as the standard format when the date and time need to be saved or calculated.
Cons: Unix timestamp format is poor readability
Workaround: Call date () to format the local time and date
Syntax Date (format,timestamp) formatting a local time and date

<?php            date_default_timezone_set (' PRC ');//Set Chinese time zone echo date ("Y-M month-D Day H:i:s");   2015-November-20th 17:30:21    ?>

---------------------------------

Description: Learn Notes

Reference: "PHP in detail"

PHP date, time-related knowledge collation

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.