Home > Backend Development > PHP Tutorial > Summary of commonly used dates and times in PHP_PHP tutorial

Summary of commonly used dates and times in PHP_PHP tutorial

WBOY
Release: 2016-07-13 16:56:29
Original
953 people have browsed it

This article summarizes some common uses of date and time in PHP. Friends in need can refer to it

1, year-month-day
echo date('Y-m-j');
2007-02-6

echo date('y-n-j');
07-2-6

Capital Y represents the four-digit year, while lowercase y represents the two-digit year;
Lowercase m represents the number of the month (with a leading), while lowercase n represents the number of the month without the leading.

echo date('Y-M-j');
2007-Feb-6

echo date('Y-m-d');
2007-02-06

Capital M represents the 3 abbreviated characters of the month, while lowercase m represents the number of the month (with leading 0);
There is no uppercase J, only lowercase j represents the day of the month, without the leading o; if a leading month is required, use a lowercase d.

echo date('Y-M-j');
2007-Feb-6

echo date('Y-F-jS');
2007-February-6th

Capital M represents the 3 abbreviated characters of the month, while capital F represents the full English version of the month. (no lowercase f)
Capital S represents the suffix of the date, such as "st", "nd", "rd" and "th", depending on the date number.

Summary:
You can use uppercase Y or lowercase y to indicate the year;
Months can be represented by uppercase F, uppercase M, lowercase m and lowercase n (two ways to represent characters and numbers respectively);
Lowercase d and lowercase j can be used to represent the day, and uppercase S represents the suffix of the date.


2. Hour:minute:second

By default, the time displayed by PHP interpretation is "Greenwich Mean Time", which is 8 hours different from our local time.

echo date('g:i:s a');
5:56:57 am

echo date('h:i:s A');
05:56:57 AM

A lowercase g indicates a 12-hour format without leading 0s, while a lowercase h indicates a 12-hour format with leading 0s.
When using the 12-hour clock, it is necessary to indicate morning and afternoon. Lowercase a represents lowercase "am" and "pm", and uppercase A represents uppercase "AM" and "PM".

echo date('G:i:s');
14:02:26

Capital G represents the hour in 24-hour format, but without leading; use capital H to represent the hour in 24-hour format with leading

Summary:
The letter g represents the hour without leading, and the letter h represents the hour with leading;
Lowercase g and h represent the 12-hour format, while uppercase G and H represent the 24-hour format.

3, leap year, week, day

echo date('L');
Whether this year is a leap year: 0

echo date('l');
Today is: Tuesday

echo date('D');
Today is: Tue

Capital L indicates whether this year is a leap year, Boolean value, returns 1 if true, otherwise 0;
The lowercase l represents the full English word for the day of the week (Tuesday);
Instead, use a capital D to represent the 3-character abbreviation of the day of the week (Tue).

echo date('w');
Today’s week: 2

echo date('W');
This week is week 06 of the year

The lowercase w represents the day of the week, and the number represents it
Capital W represents the number of weeks in the year

echo date('t');
This month is 28 days

echo date('z');
Today is the 36th day of the year

Lowercase t represents the number of days in the current month
Lowercase z indicates what day of the year today is

Convert daytime date to time timestamp

strtotime(time,now) parameter description
time specifies the time string to be parsed.
now is used to calculate the timestamp of the return value. If this parameter is omitted, the current time is used.

echo strtotime("now"), "n";
echo strtotime("10 September 2000"), "n";
echo strtotime("+1 day"), "n";
echo strtotime("+1 week"), "n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "n";
echo strtotime("next Thursday"), "n";
echo strtotime("last Monday"), "n";
?>

$str = 'Not Good';

// previous to PHP 5.1.0 you would compare with -1, instead of false
if (($timestamp = strtotime($str)) === false) {
echo "The string ($str) is bogus";
} else {
echo "$str == " . date('l dS of F Y h:i:s A', $timestamp);
}
?>

Let’s look at the strtotime example
*/
echo strtotime('2010-2-14'),"
";
echo date('Y-m-d',strtotime('2010-2-14'));

//Output value

1266076800
2010-02-14

//You should in strtotime(), you decide what can't be done. For example

# on 2/8/2010
date('m/d/y', strtotime('first day')); # 02/01/10
date('m/d/y', strtotime('last day')); # 02/28/10
date('m/d/y', strtotime('last day next month')); # 03/31/10
date('m/d/y', strtotime('last day last month')); # 01/31/10
date('m/d/y', strtotime('2009-12 last day')); # 12/31/09 - this doesn't work if you reverse the order of the year and month
date('m/d/y', strtotime('2009-03 last day')); # 03/31/09
date('m/d/y', strtotime('2009-03')); # 03/01/09
date('m/d/y', strtotime('last day of march 2009')); # 03/31/09
date('m/d/y', strtotime('last day of march')); # 03/31/10
?>


更多相关函数


date_default_timezone_set('PRC'); //默认时区
 $t = time();
 $today=date("Y-m-d",time());
 echo "今天:$today
";
 echo "某一天:".date("Y-m-d",strtotime("18 june 2008"))."
";
 echo "昨天:".date("Y-m-d",strtotime("-1 day"))."
";
 echo "明天:".date("Y-m-d",strtotime("+1 day"))."
";
 echo "一周后:".date("Y-m-d",strtotime("+1 week"))."
";
 echo "一周零两天四小时两秒后:".date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds"))."
";
 echo "下个星期四:".date("Y-m-d",strtotime("next Thursday"))."
";
 echo "上个周一:".date("Y-m-d",strtotime("last Monday"))."
";
 echo "一个月前:".date("Y-m-d",strtotime("last month"))."
";
 echo "一个月后:".date("Y-m-d",strtotime("+1 month"))."
";
 echo "十年后:".date("Y-m-d",strtotime("+10 year"))."
";
 echo "
=======================================================

";
 $w = date("w",time()); //获取今天是本周周几
 echo "今天是星期$w
";
 $d=array("日","一","二","三","四","五","六");
 $whatday="星期".$d[date("w",strtotime($today))]; //获取今天星期几
 echo "今天是$whatday
";
 $d0 = date("Y-m-d",strtotime("-$w day",$t)); //周开始
 echo "本周周日是:$d0
";
 $d6 = date("Y-m-d",strtotime((6-$w)." day",$t)); //周结束
 echo "本周周六是:$d6
";
 
 echo "本周周日是:".date("Y-m-d",strtotime("Sunday"))."
";  //周开始
 echo "本周周六是:".date("Y-m-d",strtotime("Saturday"))."
";  //周结束
 echo "上周周日是:".date("Y-m-d",strtotime("last Sunday"))."
";  //上周开始
 echo "上周周六是:".date("Y-m-d",strtotime("last Saturday"))."
";  //上周结束
 echo "
=======================================================

";
 $time = abs((strtotime("2012-12-21") - strtotime(date("Y-m-d")))/86400);//获取两个日期之间的天数差
 echo "距离世界末日还有:$time 天
";  //上周结束

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/631592.htmlTechArticle本文章总结了PHP常用的日期和时间一些常用的用法,有需要的朋友可参考一下 1,年-月-日 echo date('Y-m-j'); 2007-02-6 echo date('y-n-j'); 07-2-6 大写...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template