SoFunction
Updated on 2025-03-03

PHP time and date operation practice

Commonly used time functions:
(); //Get the total number of seconds from 1970/1/1 00:00:00 to the present

<?echo time();?>
(); //Set time

<?echo date("U",mktime(0,0,0,1,20,2011));//Output the total number of seconds from 0:00:00 on January 1, 1970 to 0:00 on January 20, 2011 //mktime parameters: mktime(hour, minute, second, month, day, year);//Constantly used in combination with date("U",mktime(hour, minute, second, month, day, year));?>
(); //Retrieve detailed information on time

<?echo date("Y-m-d H:i:s");//Output the first year of Xi - month-day hour: minute: seconds;// Please refer to the table below for filling parameters;?>

Representative string illustrate example Output
Year:
Y First year of Xi (4) <?=date("Y")?> 2011
y First year of Xi (2) <?=date("y")?> 11
moon:
m In the month, less than two people make up for zero (01~12) <?=date("m")?> 01
n In the month, less than two people will not make up for zero (1~12) <?=date("n")?> 1
t How many days will be this month (1~31) <?=date("t")?> 31
M English month name (abbreviation) <?=date("M")?> Jan
F English month name (full name) <?=date("F")?> January
day:
d Date, less than two digits to make up for zeros (01~31) <?=date("d")?> 01
j Date, less than two digits will not be supplemented (01~31) <?=date("j")?> 1
Week:
w Digital week, (0~6 represents one to day) <?=date("w")?> 0
D Week name (abbreviation) <?=date("D")?> Sun
l Week name (full name) <?=date("l")?> Sunday
hour:
H 24:00 hours, less than two digits can make up for zero (00~23) <?=date("H")?> 07
G 24:00 hours, less than two people will not make up for zero (0~23) <?=date("G")?> 7
h 12:00 hours, less than two digits can make up for zero (01~12) <?=date("h")?> 07
g 12 o'clock time, no zero will be added if less than two persons (1~12) <?=date("g")?> 7
point:
i Minutes, less than two digits make up for zero (00~59) <?=date("i")?> 40
Second:
s In seconds, less than two digits can be compensated for zero (00~59) <?=date("s")?> 45
S In seconds, less than two digits to make up for zeros (00~59) plus English order <?=date("S")?> 45th
Morning and afternoon:
A Caps AM/PM <?=date("A")?> AM
a Lowercase am/pm <?=date("a")?> am
other:
U 1970/1/1 00:00:00 Total seconds to the specified day <?=date("U")?> 1156780800
z What day of the year (0~365) <?=date("z")?> 255
*Red base represents commonly used parameters

Some common usages:
1. Obtain the total number of seconds from January 1, 1970 to today:
Copy the codeThe code is as follows:

<?
echo date("U",mktime(0,0,0,date("Y"),date("m"),date("d")));
?>

2. Get the total number of seconds from today to the first and next 7 days:
Copy the codeThe code is as follows:

<?
$setTime = date("U",mktime(0,0,0,7,20,2011));
$time = date("U",mktime(0,0,0,date("m"),date("d"),date("Y")));
$start = $time-86400*7;
$over = $time+86400*7;
if($start>=$time&&$time<=$over)
{
echo "The specified time is within 7 days before and after today";
}
else
{
echo "The specified time has exceeded 7 days before and after today";
}
?>