php教程

php获取前后日月年等的时间戳方法

我的站长站 2024-09-20 人阅读

date()函数

date('Y-m-d',timestamp); //输出年-月-日
date('Y-m-d H:i:s',timestamp); //输出年-月-日 时:分:秒

时间戳

今天:

date("Y-m-d",strtotime("today")); //strtotime('today')输出今天的开始时间戳


date("Y-m-d",time()); //time()输出当前的秒时间戳

昨天:

date("Y-m-d",strtotime("-1 day"));

明天:

date("Y-m-d",strtotime("+1 day"));

以此类推,向后几天就是+几,向前几天就是-几。比如后7天就是+7:

date("Y-m-d",strtotime("+7 day"));

周时间戳

前一周:

date("Y-m-d",strtotime("-1 week"));

后一周:

date("Y-m-d",strtotime("+1 week"));

跟天同理,向后几周就是+几,向前几周就是-几,只是把day换成week。比如后7周就是+7,:

date("Y-m-d",strtotime("+7 week"));

月时间戳

前一月:

date("Y-m-d",strtotime("-1 month"));

后一月:

date("Y-m-d",strtotime("+1 month"));

跟天同理,向后几月就是+几,向前几月就是-几,只是把day换成month。比如后7月就是+7,:

date("Y-m-d",strtotime("+7 month"));
或者:
date("Y-m-d",strtotime("last month"));

年时间戳

前一年:

date("Y-m-d",strtotime("-1 year"));

后一年:

date("Y-m-d",strtotime("+1 year"));

跟天同理,向后几年就是+几,向前几年就是-几,只是把day换成year。比如后7年就是+7,:

date("Y-m-d",strtotime("+7 year"));

一周零两天四小时五分钟两秒后时间

date("Y-m-d H:i:s",strtotime("+1 week 2 days 4 hours 5 minute 2 seconds"));

下个星期四日期

date("Y-m-d",strtotime("next Thursday"));

上周一日期

date("Y-m-d",strtotime("last Monday"));

今天起止时间戳

mktime(0,0,0,date('m'),date('d'),date('Y'));
mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;

昨天起止时间戳

mktime(0,0,0,date('m'),date('d')-1,date('Y'));
mktime(0,0,0,date('m'),date('d'),date('Y'))-1;

上周起止时间戳

mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));

本月起止时间戳

mktime(0,0,0,date('m'),1,date('Y'));
mktime(23,59,59,date('m'),date('t'),date('Y'));
相关推荐
  • 时间戳
  • Python时间戳互转

    查询时间:可查询当前时间与时间戳时间戳转时间:在前面输入框中输入时间戳点击按钮可转成时间时间转时间戳:在前面输入框中输入时间点击按钮可转成时间戳,时间格式如果不知道可复制查询时间中的时间格式import timeimport osimport tkinter as tkimport t...

    python教程 12 1年前
  • 七牛云oss时间戳图片防盗链api接口

    七牛云oss时间戳图片防盗链api接口,生成签名的 url 的时候,也要将新生成的 url 保存到 redis 中,防止每次访问都生成新的签名,即浪费服务器资源,又会增加 CDN 流量。签名代码:// Sign 每一个图片生成 2 分钟有效期的签名//// imgPath 是图片路径,不是完整链...

    php教程 140 3年前
  • php获取年月周时间戳代码

    php获取时区date_default_timezone_set("Asia/Shanghai"); date_default_timezone_set('PRC');//这两种方法效果相同时间戳转日期,可以用date(‘Y-m-s h:i:s’, 具体时间戳来实现)日期转换时间戳,用strtotime("date()").php获取时间戳//获取今...

    php教程 18 1年前
最新更新