PHP time stamp and date format conversion (reprint)

Source: Internet
Author: User
Conversion of timestamps and date formats in PHP (reprint)
One, the PHP timestamp function gets the UNIX timestamp of the specified date Strtotime ("2009-1-22″) example is as follows:

Echo strtotime ("2009-1-22″) Results: 1232553600

Description: Return January 22, 2009 0:0 0 seconds timestamp

Two, php timestamp function get the English text date time example is as follows:

Easy to compare, use date to convert time stamp to system time with specified timestamp

(1) Print the timestamp for tomorrow at this time Strtotime ("+1 Day")

Current Time: Echo Date ("Y-m-d h:i:s," time ()) Results: 2009-01-22 09:40:25

Time Specified: Echo date ("Y-m-d h:i:s", Strtotime ("+1 Day") Results: 2009-01-23 09:40:25

(2) Print the timestamp at this time yesterday Strtotime ("-1 day")

Current Time: Echo Date ("Y-m-d h:i:s," time ()) Results: 2009-01-22 09:40:25

Time Specified: Echo date ("Y-m-d h:i:s", Strtotime ("1 day") Results: 2009-01-21 09:40:25

(3) Print timestamp strtotime ("+1 Week") at this time next week

Current Time: Echo Date ("Y-m-d h:i:s," time ()) Results: 2009-01-22 09:40:25

Time Specified: Echo date ("Y-m-d h:i:s", Strtotime ("+1 Week") Results: 2009-01-29 09:40:25

(4) Print timestamp strtotime ("1 week") at this time last week

Current Time: Echo Date ("Y-m-d h:i:s," time ()) Results: 2009-01-22 09:40:25

Time Specified: Echo date ("Y-m-d h:i:s", Strtotime ("1 Week") Results: 2009-01-15 09:40:25

(5) Print the time stamp specified next week strtotime ("next Thursday")

Current Time: Echo Date ("Y-m-d h:i:s," time ()) Results: 2009-01-22 09:40:25

Time Specified: Echo date ("Y-m-d h:i:s", Strtotime ("next Thursday") Results: 2009-01-29 00:00:00

(6) Print the timestamp strtotime ("last Thursday") specified on the previous day of the week

Current Time: Echo Date ("Y-m-d h:i:s," time ()) Results: 2009-01-22 09:40:25

Specified time: echo date ("Y-m-d h:i:s", Strtotime ("last Thursday")) Results: 2009-01-15 00:00:00
The PHP timestamp Function example above shows that Strtotime can parse the datetime description of any English text into a Unix timestamp, and we get the specified timestamp by combining mktime () or date () format date time to achieve the required date time.

Example:


/***************************************************************************
*
* Copyright (c) Baidu.com, Inc. All Rights Reserved
* $Id $
*
**************************************************************************/



Time Stamp to date
$date _time_array = getdate (1297845628); 1311177600 1316865566
$hours = $date _time_array["Hours"];
$minutes = $date _time_array["Minutes"];
$seconds = $date _time_array["seconds"];
$month = $date _time_array["Mon"];
$day = $date _time_array["Mday"];
$year = $date _time_array["year"];

echo "Year: $year \nmonth: $month \nday: $day \nhour: $hours \nminutes: $minutes \nseconds: $seconds \ n";

Normal date turn timestamp
Echo mktime (0, 0, 0, 9, 18, 2011). "\ n";
Echo mktime (0, 0, 0, 9, 25, 2011). "\ n";

/*
Time ();
is to get the current time, but to get an integer type
*/
This can be formatted
echo "Time () displays the day of the month and seconds:". Date ("Y-m-d h:i:s", Time ()). "\ n";
So even when the seconds show together
echo "Time () displays only month Day:". Date ("Y-m-d", Time ()). " \ n "; Year-only date

echo "timestamp format:". Date ("Y-m-d h:i:s", 1297845628). "\ n"; Using timestamps directly

Turn the normal date into a timestamp, and if it is sometimes the same in seconds,

$year = ((int) substr ("2008-12-04", 0,4));//year acquired

$month = ((int) substr ("2008-12-04", 5,2));//Get month

$day = ((int) substr ("2008-12-04", 8,2));//Date

Echo mktime (0,0,0, $month, $day, $year);

/* Vim:set ts=4 sw=4 sts=4 tw=100 noet: */
?>


parts to be aware of
<一> There are two types of PHP time, one is the timestamp type (1228348800), the second is the normal date format (2008-12-4)

, and the time stamp above php5.1 is 8 hours from the actual time, the solution is as follows
1, the simplest The law is not to use php5.1 above version-obviously this is undesirable Method!!!

2, modify php.ini. Open php.ini Find Date.timezone remove the previous semicolon = after adding Asia/shanghai, restart Apache server can--the disadvantage is that if the program
put on someone else's server, can not modify the php.ini, then do not.

3, add the initialization statement of the time in the program that is: "Date_default_timezone_set (" Asia/shanghai "); "This can be arbitrarily set by the programmer, my push
recommendation."
Time zone identifiers, values available for mainland China are: prc,asia/chongqing, Asia/shanghai, Asia/urumqi (Chinese, Chongqing, Shanghai, Urumqi), Etc/gmt-8,asia/harbin
Taiwan available: Asia/macao, Asia/hong_kong, Asia/taipei (Macau, Hong Kong, Taipei)
and Singapore: Asia/singapore

This will allow the output to be made in Beijing time br>


  • [/two]/one

    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.