PHP的strtotime函数是一个强大的函数,能够将任何有意义的日期字符串转换成Unix时间戳。它可以接受英文字符串,如“next Monday”,“last Sunday”,“+1 week”,“+1 day”,“+1 year”等,也可以接受标准的日期格式,如“2020-11-30”,“2020/11/30”,“2020.11.30”,“30/11/2020”,“30.11.2020”等。
使用方法
strtotime函数的使用方法非常简单,只需要在函数中传入一个有意义的日期字符串即可,函数会返回一个Unix时间戳。
$timestamp = strtotime("2020-11-30");
echo $timestamp;
// Output: 1606924800
示例代码
下面是一些关于strtotime函数的示例代码,可以帮助理解它的用法。
- 获取当前时间的Unix时间戳:
$timestamp = strtotime("now"); echo $timestamp; - 获取本周一的Unix时间戳:
$timestamp = strtotime("Monday this week"); echo $timestamp; - 获取下周一的Unix时间戳:
$timestamp = strtotime("Monday next week"); echo $timestamp; - 获取上个月一天的Unix时间戳:
$timestamp = strtotime("last day of last month"); echo $timestamp; - 获取下个月第一天的Unix时间戳:
$timestamp = strtotime("first day of next month"); echo $timestamp; - 获取今天加上一年的Unix时间戳:
$timestamp = strtotime("+1 year"); echo $timestamp; - 获取明天减去两个月的Unix时间戳:
$timestamp = strtotime("tomorrow -2 months"); echo $timestamp;
strtotime函数是一个非常实用的函数,可以帮助我们快速将有意义的日期字符串转换成Unix时间戳,从而方便我们进行日期操作。