PHP strftime function Get date time (switch usage)

Source: Internet
Author: User
Tags echo date locale setting month name
Strftime is a common PHP in the date-time acquisition function, will be stored in the database of the number converted to a function of time, here for you to introduce the use of strftime function and parameters, the need for friends can refer to the next

The use of the strftime () function

The strftime () function converts date strings in YYYY-MM-DD HH:MM:SS format to other forms of strings.
The syntax of Strftime () is strftime (format, date/time, modifier, modifier, ...)

The function strftime () operates somewhat like sprintf (): Identifies a collection of formatting commands that begin with a percent sign (%), and the formatted output is placed in a string. The format command describes the exact representation of the various date and time information in the string strdest. Other characters in the format string are placed in the string as is. The format commands are listed below, and they are case-sensitive.

Strftime () Definition and usage
The strftime () function formats the local time/date according to the locale setting.

Strftime () syntax
Strftime (format,timestamp) parameter description
Format is optional. Specifies how the results are returned.
Timestamp is optional.
Hints and Notes
Tip: The same behavior as Gmstrftime (), the difference is that the return time is local time.

It can be formatted with the following symbols for dates and times:

%a shorthand for the day of the week
%A full name of the week
Abbreviated%B Month
Full name of the%B month
Time string for the date of%c standard
First two digits of the%c year
The day ordinal of a month in%d decimal notation
%d Month/day/year
%e the day ordinal of a month in a two-character field, in decimal notation
%F year-month-day
%g two digits of the year, using week-based
%G year, using week-based
%h Abbreviated month name
%H 24-Hour Hour
%I 12-Hour Hour
%j decimal indicates the day ordinal of the year
%m the month represented by decimal
%M minutes in a 10 o'clock-hour representation
%n New Line character
%p equivalent display of the local AM or PM
%r 12 hours of time
%R display hours and minutes: hh:mm
%s number of seconds in decimal
%t Horizontal Tab
%T display time seconds: hh:mm:ss
%u days of the week, Monday for the first day (values from 1 to 7, Monday to 1)
%u year of the week, put Sunday as the first day (value from 0 to 53)
%V Week of the year, using week-based
%w Decimal Day of the week (value from 0 to 6, Sunday is 0)
%W Week of the year, Monday as the first day (value from 0 to 53)
Date string for%x standard
Time string for%x standard
%y decimal Year without century (values from 0 to 99)
%Y 10 year with century part
%z,%z the time zone name and returns a null character if the time zone name cannot be obtained.
% output percent

An example of the use of strftime () is as follows:

Select Strftime ('%y-%m-%d%h:%m:%s ', ' Now ', ' localtime ');
Results: 2018-5.15 23:58:09
In fact, the better usage is this, such as the system, this month or this year's expenditure:
Select Strftime ('%y-%m ', date) as month, sum (expense) as monthly expenditure from the Flow Account group by month;

Switch usage

<?phpsetlocale (Lc_time, "CHS");     Set Local Environment $weekday = strftime ("%A");     Declare the value of the variable $weekday, get the system time and just need to get the day of the Week switch ($weekday) {     //switch statement, determine the value of the $weekday case "Monday":     //If the value of the variable is "Monday" echo "Today is $weekday, a new day begins!" "; Break Case "Tuesday":     //If the value of the variable is "Tuesday" echo "Today is $weekday, serious work attitude is really important!"; break; case "Wednesday":     //If the value of the variable is "Wednesday" echo "Today is $ Weekday, enrich life, work hard! "; Break Case "Thursday":     //If the value of the variable is "Thursday" echo "Today is $weekday, diligence can create performance, refueling! )"; Break Case "Friday":     //If the value of the variable is "Friday" echo "Today is $weekday, actively complete the task!" "; Break Case "Saturday":     //If the value of the variable is "Saturday" echo "Today is $weekday, can relax the mood!" "; Break Default:      //Defaults echo "Today is $weekday, go happy!" "; break;}? >

The following script's small series continues to add some examples:

<?phpecho (Strftime ("%b%d%Y%x", Mktime (20,0,0,12,31,98))), Echo (Gmstrftime ("%b%d%Y%x", Mktime (20,0,0,12,31,98)) );//Output The current date, time, and time zone echo ("It is%a on%b%d,%Y,%x gmstrftime zone:%Z", Times ());? >

Output:

Dec 31 1998 20:00:00
Dec 31 1998 19:00:00

Get the Unix timestamp strtotime ("2009-1-22") for the specified date as shown in the following example:
Echo strtotime ("2009-1-22") Results: 1232553600
Description: Return January 22, 2009 0:0 0 seconds timestamp

Second, get the English text date and time examples are 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

Related recommendations:

PHP's use of composer

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.