PHP | strotime()函数

strtotime()函数是PHP中的内置函数,用于将英文文本日期时间描述转换为UNIX时间戳。该函数接受一个英文字符串参数,该参数表示日期时间的描述。例如,“现在”是指英语日期时间描述中的当前日期。该函数返回自 时间戳 .我们可以使用date()函数以日期格式返回英文文本日期时间。

null

语法:

strtotime ($EnglishDateTime, $time_now)

参数: 该函数接受两个参数,如下所示:

  1. $EnglishDateTime –此参数指定英文文本日期时间描述,表示要返回的日期或时间。函数解析字符串并以秒为单位返回时间。该参数是必需的
  2. $time_现在 此参数指定用于计算返回值的时间戳。这是一个可选参数。

注: 由于时间/日期不是静态的,因此输出会有所不同。

下面的程序演示了strtotime()函数。

项目1: 下面的程序演示了strotime() 当英文文本为“now”时起作用。

<?php
// PHP program to demonstrate the strtotime()
// function when the english text is "now"
// prints current time in second
// since now means current
echo strtotime ( "now" ), "" ;
// prints the current time in date format
echo date ( "Y-m-d" , strtotime ( "now" )). "" ;
?>


输出:

1525378260
2018-05-03

项目2: 下面的程序演示了strotime() 当英文文本为日期时,函数。

<?php
// PHP program to demonstrate the strtotime()
// function when the english text is a date
// prints the converted english text in second
echo strtotime ( "12th february 2017" ), "" ;
// prints the above time in date format
echo date ( "Y-m-d" , strtotime ( "12th february 2017" )). "" ;
?>


输出:

1486857600
2017-02-12

方案3: 下面的程序演示了strotime() 当英文文本对应于任何一天时。

<?php
// PHP program to demonstrate the strtotime()
// function when the english text corresponds to any
// day
// prints the converted english text in second
echo strtotime ( "next sunday" ), "" ;
// prints the above time in date format
echo date ( "Y-m-d" , strtotime ( "next sunday" )). "" ;
?>


输出:

1525564800
2018-05-06

PHP是一种专门为web开发设计的服务器端脚本语言。通过以下步骤,您可以从头开始学习PHP PHP的教程 PHP示例 .

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享