Heim  >  Artikel  >  php教程  >  [PHP] 重回基础(date函数和strtotime函数) - 陶士涵

[PHP] 重回基础(date函数和strtotime函数) - 陶士涵

WBOY
WBOYOriginal
2016-05-20 10:14:30883Durchsuche

 

date():格式化一个本地时间或者日期,当前时间 2016年5月13日 15:19:49

 

使用函数date(),输出当前是月份中的第几天,参数:String类型 d

例如:echo date("d"); 输出 13

 

使用函数date(),输出当前是星期中的第几天,参数:String类型 D或者 N

例如:echo date("D"); 输出 Fri

echo date("N"); 输出 5

echo date("l"); 输出 Friday

 

使用函数date(),输出当前月份中的第几月,参数:String类型 n

echo date("n"); 输出 5

 

使用函数date(),判断当前年份是否是闰年,参数:String类型 L

echo date("L"); 输出 1

 

 

 

strtotime():把字符串类型日期格式转成时间戳

使用函数strtotime(),打印前一天日期,参数:String类型 “-1 day”

echo date("Y-m-d H:i:s",strtotime("-1day"));输出 2016-05-12 15:27:33

 

使用函数strtotime(),打印明天日期,参数:String类型 “+1 day”

echo date("Y-m-d H:i:s",strtotime("+1 day"));输出 2016-05-14 15:28:29

 

使用函数strtotime(),打印下周日期,参数:String类型 “+1 week”

echo date("Y-m-d H:i:s",strtotime("+1 week"));;输出 2016-05-20 15:29:35

 

使用函数strtotime(),打印下一个月日期,参数:String类型 “+1 month”

echo date("Y-m-d H:i:s",strtotime("+1 month")); 输出:2016-06-13 15:37:42

 

使用函数strtotime(),打印下周一日期,参数:String类型 “last Mondy”

echo date("Y-m-d H:i:s",strtotime("next Monday")); 输出:2016-05-16 00:00:00

 

使用函数strtotime(),打印下周零两天两小时两秒后日期,参数:String类型组合一下

echo date("Y-m-d H:i:s",strtotime("+1 week 2 day 2 hour")); 输出 2016-05-22 17:34:34

 

==================================================================

java版:

java.util.Date类

获取Date对象,new出来

调用Date对象的getTime()方法,获取时间戳(毫秒值)

 

java.text.SimpleDateFormat类

获取SimpleDateFormat对象,new出来,构造参数:"yyyy-MM-dd hh:mm:ss"

调用SimpleDateFormat对象的format()方法,获取String类型的日期,参数:Date对象

 

例如:

                   Date date=new Date();

                   SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

                   System.out.println(format.format(date));

 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn