How to Strtotime timestamp when encountering the Unix epoch problem (how to use the DateTime class)

Source: Internet
Author: User
Tags diff epoch time unix epoch time

Unix epoch Time, also known as POSIX time/epoch time, is the total number of seconds from 0 seconds from January 1, 1970 0:0 UTC to the present, excluding leap seconds. A positive value represents 1970, and a negative value indicates before 1970.

Unix 2038 Bug (y2k38 vulnerability)

Also known as the Unix Millennium Bug, this vulnerability would affect all 32-bit systems using a UNIX timestamp integer to record the time of PHP, and other programming languages. The maximum time a variable of an integral type can be saved is January 19, 2038 03:14:07. After this time, the integer value will overflow. From January 01, 1970 onwards, to the world standard Time January 19, 2038 Tuesday 03:14:07 more than 2^31–1. 2^31–1 is 0x7fffffff, I believe many programmers have seen, in 32-bit systems, this represents the largest signed integer. If it is used to represent the number of seconds, it is probably equivalent to 68.1 years, which is exactly the number from 1970 to 2038.

Strtotime () and Mktime ()

Both functions in PHP are affected by the Unix 2038 bug, which returns False when the date value is greater than 2038 and returns a negative value when it is less than 1970.

DateTime class

PHP has introduced a DateTime class from version 5.2 to address the y2k38 vulnerability, and the following are the methods of the DateTime class:

DatetimeImplementsDatetimeinterface {/*Constants*/Const stringATOM = "Y-M-D\TH:I:SP" ;Const stringCOOKIE = "L, d-m-y h:i:s T" ;Const stringISO8601 = "Y-m-d\th:i:so" ;Const stringRFC822 = "D, d M y h:i:s O" ;Const stringRFC850 = "L, d-m-y h:i:s T" ;Const stringRFC1036 = "D, d M y h:i:s O" ;Const stringRFC1123 = "D, D M Y h:i:s O" ;Const stringRFC2822 = "D, D M Y h:i:s O" ;Const stringRFC3339 = "Y-M-D\TH:I:SP" ;Const stringRSS = "D, D M Y h:i:s O" ;Const string"Y-m-d\th:i:sp" ;/*Method*/ Public__construct ([string $time= "Now" [, Datetimezone$timezone=NULL ]] ) PublicDateTime Add (DateInterval$interval ) Public StaticDateTime Createfromformat (string $format,string $time[, Datetimezone$timezone=Date_default_timezone_get ()]) Public Static Arraygetlasterrors (void) PublicDateTime Modify (string $modify ) Public StaticDateTime __set_state (Array $array ) PublicDateTime setDate (int$yearInt$monthInt$day ) PublicDateTime setisodate (int$yearInt$week[, int$day= 1 ] ) PublicDateTime settime (int$hourInt$minute[, int$second= 0 ] ) PublicDateTime Settimestamp (int$unixtimestamp ) PublicDateTime Settimezone (Datetimezone$timezone ) PublicDateTime Sub (dateinterval$interval ) PublicDateInterval diff (Datetimeinterface$datetime 2[, BOOL$absolute=false ] ) Public stringFormat (string $format ) Publicint getoffset (void) Publicint Gettimestamp (void) Publicdatetimezone gettimezone (void) Public__wakeup (void)}

Common methods for summarizing datetime classes

1. Output Current time

$date _time New DateTime (); Echo $date _time->format (' y-m-d h:i:s ');

The output is: 2016-09-12

2, output a given time

$date _time New DateTime (' 2016-09-12 '); Print_r ($date _time); // the output is: Object ([date] = 2016-06-13 00:00:00.000000 [Timezone_type] = 3 [timezone] + PRC)

3, according to the given time format to the time required

$date _time = Datetime::createfromformat (' Ymd ', ' 20160912 '); Echo $date _time->format (' y-m-d ');

4. Output UNIX Timestamp

$date _time New DateTime (' 2016-09-12 '); Echo $date _time->format (' U ');

5. Format date according to the given time stamp output

$date _time New DateTime (); $date _time->settimestamp (1473609600); Echo $date _time->format (' y-m-d h:i:s ');

6, two dates to compare, year and year, month and month, day and day ....

$start _time=NewDateTime (' 2016-09-12 00:00:00 ');$end _time=NewDateTime (' 3016-09-12 00:00:00 ');$inberval=$start _time->diff ($end _time);Print_r($inberval);//the output is:DateIntervalObject([y]=> 1000[M]=> 0[d]=> 0[h]=> 0[i]=> 0[s]=> 0[Weekday]=> 0[Weekday_behavior]=> 0[first_last_day_of]=> 0[Invert]=> 0[Days]=> 365242[Special_type]=> 0[Special_amount]=> 0[Have_weekday_relative]=> 0[Have_special_relative]=> 0)

7. Date plus minus

The argument to the DateInterval constructor is a string that represents the time interval convention, which begins with the letter p, followed by an integer, and finally a period identifier, qualifying the preceding integer. Valid period identifiers are as follows: Y (year), M (month), D (Day), W (week), H (Time), M (min), s (seconds), interval conventions can have either a date or time, with the letter T split. For example: Interval two days 5 hours, p2dt5h2m;

        $date _time=NewDateTime (); $interval=NewDateInterval (' p2dt5h2m '); $date _time->add ($interval); Echo $date _time->format (' y-m-d h:i:s '). ' <br> '; $interval= Dateinterval::createfromdatestring (' +1 month '); $date _time->add ($interval); Echo $date _time->format (' y-m-d h:i:s '). ' <br> '; $date _time->sub ($interval); Echo $date _time->format (' y-m-d h:i:s ');

8. Reset the date of the current time

        $date _time New DateTime ();         $date _time->setdate (2015,10,1);         Echo $date _time->format (' y-m-d ');    

9. Reset the time portion of the current time

        $date _time New DateTime ();         $date _time->settime (13,10,1);         Echo $date _time->format (' y-m-d h:i:s ');

10. Iteration of the date

By implementing a date iteration through the Dateperiod class, it constructs a method that accepts four parameters:
1. Date instance, indicating the date and time when the iteration started;
2, dateinterval instance, indicates the next date and time interval;
3, the number of times, indicating the number of iterations;
4, optional parameters, explicitly specify the end date and time of the period. If the start date and time are excluded from the iteration, it can be set to dateperiod::exclude_start_date);
        $date _time=NewDateTime (); $interval= Dateinterval::createfromdatestring ('-1 day ')); $period=NewDateperiod ($date _time,$interval, 3,dateperiod::exclude_start_date); foreach($period  as $date) {            Echo $date->format (' y-m-d '),Php_eol; }

How to Strtotime timestamp when encountering the Unix epoch problem (how to use the DateTime class)

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.