PHP strtotime函数的用法和示例代码

分类:知识百科 日期: 点击:0

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时间戳,从而方便我们进行日期操作。

标签:

版权声明

1. 本站所有素材,仅限学习交流,仅展示部分内容,如需查看完整内容,请下载原文件。
2. 会员在本站下载的所有素材,只拥有使用权,著作权归原作者所有。
3. 所有素材,未经合法授权,请勿用于商业用途,会员不得以任何形式发布、传播、复制、转售该素材,否则一律封号处理。
4. 如果素材损害你的权益请联系客服QQ:77594475 处理。