php strftime function to get date time of switch usage

  • 2021-10-13 06:48:33
  • OfStack

Usage of strftime () Function

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

The function strftime () operates somewhat like sprintf (): It identifies a set of formatting commands starting with a percent sign (%), and the formatted output is placed in a string. Format commands describe the exact representation of various date and time information in the string strDest. Other characters in the format string are put into the string as they are. Format commands are below, and they are case sensitive.

strftime () Definition and usage
The strftime () function formats the local time/date based on the locale.

strftime () Syntax
Parameter description of strftime (format, timestamp)
format is optional. Specifies how the results are returned.
timestamp is optional.
Tips and Notes
Tip: It behaves the same as gmstrftime () except that the return time is local time.

It can format dates and times with the following symbols:

% a abbreviation for days of the week
% A the full name of the day of the week
Abbreviation for month% b
Full name of month% B
Time series of dates for% c standard
The first two digits of% C year
% d Day ordinal of the month in decimal
% D months/days/years
% e the day ordinal of the month in decimal in a two-character field
% F year-month-day
The last two digits of% g year, using week-based year
% G year, using week-based year
% h abbreviated month name
% H 24-hour hours
% I 12-hour hours
% j Day ordinal of a year in decimal
% m month in decimal
Minutes in% M 10
% n new line character
% p local AM or PM equivalent display
% r 12 hours of time
% R shows hours and minutes: hh: mm
% S seconds in decimal
% t horizontal tab
% T display hours and seconds: hh: mm: ss
% u Day of the week, Monday 1 is Day 1 (values range from 1 to 7, Monday 1 is 1)
% U week of the year with Sunday as day 1 (values from 0 to 53)
% V week ordinal of a year, using week-based years
% w day of the week in decimal (values from 0 to 6, Sunday 0)
% W week of the year with Monday as day 1 (values from 0 to 53)
% x standard date string
% X standard time string
% y decimal year without century (values from 0 to 99)
% Y 10 years with century part
% z,% Z time zone name, null character if no time zone name is available.
%% Output percent sign

Examples of the use of strftime () are as follows:

select strftime('%Y-%m-%d %H:%M:%S','now','localtime');
Results: 2018-5.15 23:58: 09
In fact, a better usage is like this, for example, the expenditure this month or this year:
select strftime ('% Y-% m', Date) as, sum (Expenditure) as Month Expenditure from Journal group by Month;

switch Usage


<?php
setlocale(LC_TIME,"chs");     // Setting up the local environment 
$weekday = strftime("%A");     // Declare variables $weekday Gets the system time and only needs to get the day of the week 
switch ($weekday){     //switch Statement, judgment $weekday Value of 
 case " Week 1":     // If the value of the variable is week 1 " 
 echo " Today is $weekday  , new 1 The sky has begun! ";
 break;
 case " Week 2":     // If the value of the variable is week 2 " 
 echo " Today is $weekday  Serious work attitude is really important !";
 break;
 case " Week 3":     // If the value of the variable is week 3 " 
 echo " Today is $weekday  Enrich your life and work hard! ";
 break;
 case " Week 4":     // If the value of the variable is week 4 " 
 echo " Today is $weekday  Diligence can create performance, come on!) ";
 break;
 case " Week 5":     // If the value of the variable is week 5 " 
 echo " Today is $weekday  , actively complete the task! ";
 break;
 case " Week 6":     // If the value of the variable is week 6 " 
 echo " Today is $weekday  , you can relax! ";
 break;
 default:      // Default value 
 echo " Today is $weekday ,  Go happy1 Down! ";
 break;
}
?>

The following site this site continues to add some examples for you:


<?php
echo(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(gmstrftime("It is %a on %b %d, %Y, %X time zone: %Z",time()));
?>

Output:

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

Gets the unix timestamp strtotime ("2009-1-22") for the specified date as follows:
echo strtotime ("2009-1-22") Results: 1232553600
Description: Returns January 22, 2009 0: 0:0: 0sec timestamp

2. An example of getting English text date and time is as follows:
For easy comparison, use date to convert the current timestamp and the specified timestamp into system time

(1) Print the timestamp strtotime ("1 day") at this time tomorrow
Current time: echo date ("Y-m-d H: i: s", time ()) Result: 2009-01-22 09:40:25
Specified time: echo date ("Y-m-d H: i: s", strtotime ("1 day")) Result: 2009-01-23 09:40:25

(2) Print the timestamp strtotime ("-1 day") at this time yesterday
Current time: echo date ("Y-m-d H: i: s", time ()) Result: 2009-01-22 09:40:25
Specified time: echo date ("Y-m-d H: i: s", strtotime ("-1 day")) Result: 2009-01-21 09:40:25

(3) Print the timestamp strtotime ("1 week") at this time of the next week
Current time: echo date ("Y-m-d H: i: s", time ()) Result: 2009-01-22 09:40:25
Specified time: echo date ("Y-m-d H: i: s", strtotime ("1 week")) Result: 2009-01-2909:40:25

(4) Print the timestamp strtotime ("-1 week") at this time of last week
Current time: echo date ("Y-m-d H: i: s", time ()) Result: 2009-01-22 09:40:25
Specified time: echo date ("Y-m-d H: i: s", strtotime ("-1 week")) Result: 2009-01-15 09:40:25

(5) Print the timestamp strtotime ("next Thursday") specifying the next day of the week
Current time: echo date ("Y-m-d H: i: s", time ()) Result: 2009-01-22 09:40:25
Specified time: echo date ("Y-m-d H: i: s", strtotime ("next Thursday")) Result: 2009-01-29 00: 00:00

(6) Print the timestamp strtotime ("last Thursday") specifying the day of the week
Current time: echo date ("Y-m-d H: i: s", time ()) Result: 2009-01-22 09:40:25
Specified time: echo date ("Y-m-d H: i: s", strtotime ("last Thursday")) Result: 2009-01-1500: 00:00


Related articles: