网络编程
位置:首页>> 网络编程>> php编程>> PHP获取特殊时间戳的方法整理

PHP获取特殊时间戳的方法整理

作者:于先生吖  发布时间:2023-05-25 00:47:36 

标签:PHP,特殊,时间戳

问题描述

时间在我们日常的代码编写中会是经常出现的筛选或排序条件,尤其是一些特殊时间节点的时间显得尤为突出,例如昨天,当前日期,当前月份,当前季度,以及当前年份的开始以及结束的时间戳,今天对部分相对简便的方法进行了部分整理。

解决方案

话不多说,稍微进行分类,贴代码。

今天时间戳与日期

时间戳

当前天的时间戳直接使用当前时间格式,指定起始以及结束时间来实现快速拿到时间戳的效果。

$startTime = strtotime(date('Y-m-d').'00:00:00');
$overTime = strtotime(date('Y-m-d').'23:59:59');

日期格式

相应的,咱们可以直接字符串拼接实现日期格式的显示。

//弱类型语言,直接拼接字符串
$startDate=date('Y-m-d').' 00:00:00';
$overDate=date('Y-m-d').' 00:00:00';

昨天时间戳与日期

时间戳

$startTime = mktime(0,0,0,date('m'),date('d')-1,date('Y'));
$overTime = mktime(0,0,0,date('m'),date('d'),date('Y'))-1;

日期格式

方法一: 根据时间戳转日期格式

//根据上面的时间戳进行格式转换
$startDate=date("Y-m-d H:i:s",$startTime);
$overDate =date("Y-m-d H:i:s",$overTime);

新想法:根据首先获取当前天日期,然后使用date函数进行时间格式转换

//获取当前日期的天数的数值减一之后就是昨天啦
$time=date('d')-1;
$startDate=date("Y-m-".$time." 00:00:00",time());
$overDate=date("Y-m-".$time." 23:59:59",time());

但是在月初时会出现日期为0的异常,除了进行判断,不知道有没有其他简便的方法可以解决,不然还是时间戳转日期格式比较简便,希望有简单解决办法的大佬给点新想法。

本周时间戳与日期

时间戳

date( )函数中 date(‘w’) 可以获取今天是本周第几天,通过简单处理就可以得到本周的起始以及结束时间。

这种思路和方法可以推广到上周的起始和结束时间。

方法一:

//本周开始时间戳
$startTime = mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y'));
//本周结束时间戳
$overTime = mktime(23,59,59,date('m'),date('d')-date('w')+7,date('y'));

方法二:

$nowDate = date("Y-m-d");
$week = date('w',strtotime($nowDate));
$startTime = strtotime("$nowDate -".($week ? $week - 1 : 6).' days');//本周第一天
$overTime = $start_time + 86400*7 -1; //本周最后一天

日期格式

使用日期格式函数转换时间戳,也可以用上面的方法进行date()函数中格式,进行转换。

//本周开始时间戳
$startTime = date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y')));
//本周结束时间戳
$overTime = date("Y-m-d H:i:s",mktime(23,59,59,date('m'),date('d')-date('w')+7,date('y')));

本月时间戳和日期

时间戳

//本月起始时间时间戳
$startTime =mktime(0,0,0,date('m'),1,date('Y'));  
//本月结束时间时间戳
$overTime =mktime(23,59,59,date('m'),date('t'),date('Y'));

日期格式

使用date( )函数进行时间戳转换日期格式。

//本月起始时间日期格式
$startTime = date("Y-m-d H:i:s",mktime(0,0,0,date('m'),1,date('Y')));  
//本月结束时间日期格式
$overTime = date("Y-m-d H:i:s",mktime(23,59,59,date('m'),date('t'),date('Y')));

本季度时间戳和日期

时间戳

//获取当前季度
$season = ceil((date('m'))/3);
//当前季度开始时间戳
$startTime = mktime(00,00,00,$season*2+1,1,date('Y'));
//获取当前季度结束时间戳
$overTime = mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'));

日期格式

把上面的时间戳转换为日期格式

date("Y-m-d",$startTime)
date("Y-m-d",$overTime)

当前年时间戳和日期

时间戳

//本年开始  
$startTime  = strtotime(date("Y",time())."-1"."-1");
//本年结束
$overTime  = strtotime(date("Y",time())."-12"."-31");  

日期格式

//本年开始  
$startTime  = date("Y-m-d H:i:s",strtotime(date("Y",time())."-1"."-1"));
//本年结束
$overTime  =  date("Y-m-d H:i:s",strtotime(date("Y",time())."-12"."-31"));  

strtotime函数获取描述对应时间

明天当前时间

strtotime("+1 day")//时间戳

date("Y-m-d H:i:s",strtotime("+1 day"))//日期格式

昨天当前时间

strtotime("-1 day")//时间戳

date("Y-m-d H:i:s",strtotime("-1 day"))//日期格式

下个星期当前时间

strtotime("+1 week")//时间戳

date("Y-m-d H:i:s",strtotime("+1 week"))//日期格式

上个星期当前时间

strtotime("-1 week")//时间戳

date("Y-m-d H:i:s",strtotime("-1 week"))//日期格式

下星期几当前时间

strtotime("next Thursday")//时间戳

date("Y-m-d H:i:s",strtotime("next Thursday"))//日期格式

上星期几当前时间

strtotime("last Thursday")//时间戳

date("Y-m-d H:i:s",strtotime("last Thursday"))//日期格式

时间戳转日期格式

$timestamp =1664170375;//当前时间戳
date("Y-m-d H:i:s",$timestamp);//转换为日期格式

日期格式转时间戳

$time = '2022-09-26 23:31:59';//时间格式参数
strtotime($time);//转换为时间戳

获取特定时间戳函数

/**特定时间戳函数
* @param $targetTime
*/
function gettimestamp($targetTime){
   switch ($targetTime){
       case 'today'://今天
           $timeamp['start'] = strtotime(date('Y-m-d'));
           $timeamp['over'] = strtotime(date('Y-m-d',strtotime('+1 day')));
           break;
       case 'yesterday'://昨天
           $timeamp['start'] = strtotime(date('Y-m-d',strtotime('-1 day')));
           $timeamp['over'] = strtotime(date('Y-m-d'));
           break;
       case 'beforyesterday'://前天
           $timeamp['start'] = strtotime(date('Y-m-d',strtotime('-2 day')));
           $timeamp['over'] = strtotime(date('Y-m-d',strtotime('-1 day')));
           break;
       case 'beforweek'://本周
           $timeamp['start'] = strtotime(date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"))));
           $timeamp['over'] = strtotime(date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"))));
           break;
       case 'nowmonth'://本月
           $timeamp['start'] = strtotime(date('Y-m-01'));
           $timeamp['over'] = strtotime(date('Y-m-d',strtotime('+1 day')));
           break;
       case 'permonth'://上月
           $timeamp['start'] = strtotime(date('Y-m-01',strtotime('-1 month')));
           $timeamp['over'] = strtotime(date('Y-m-01'));
           break;
       case 'preweek'://上周 注意我们是从周一开始算
           $timeamp['start'] = strtotime(date('Y-m-d',strtotime('-2 week Monday')));
           $timeamp['over'] = strtotime(date('Y-m-d',strtotime('-1 week Monday +1 day')));
           break;
       case 'nowweek'://本周
           $timeamp['start'] = strtotime(date('Y-m-d',strtotime('-1 week Monday')));
           $timeamp['over'] = strtotime(date('Y-m-d',strtotime('+1 day')));
           break;
       case 'preday'://30
           $timeamp['start'] = strtotime(date('Y-m-d'),strtotime($param.' day'));
           $timeamp['end'] = strtotime(date('Y-m-d'));
           break;
       case 'nextday'://30
           $timeamp['start'] = strtotime(date('Y-m-d'));
           $timeamp['over'] = strtotime(date('Y-m-d'),strtotime($param.' day'));
           break;
       case 'preyear'://去年
           $timeamp['start'] = strtotime(date('Y-01-01',strtotime('-1 year')));
           $timeamp['over'] = strtotime(date('Y-12-31',strtotime('-1 year')));
           break;
       case 'nowyear'://今年
           $timeamp['start'] = strtotime(date('Y-01-01'));
           $timeamp['over'] = strtotime(date('Y-m-d',strtotime('+1 day')));
           break;
       case 'quarter'://季度
           $quarter = ceil((date('m'))/3);
           $timeamp['start'] = mktime(0, 0, 0,$quarter*3-2,1,date('Y'));
           $timeamp['over'] = mktime(0, 0, 0,$quarter*3+1,1,date('Y'));
           break;
       default:
           $timeamp['start'] = strtotime(date('Y-m-d'));
           $timeamp['over'] = strtotime(date('Y-m-d',strtotime('+1 day')));
           break;
   }
   return $timeamp;
}

写在最后

小发现:在进行测试的时候发现了 date()函数比较有意思的地方,可以直接拼接结果,当你把y-m-d h:i:s中的一部分写死后仍然是可以执行的,结果就是你写死的数值,后面有机会深入研究下底层代码,好像是在C语言中,结构体来实现日期以及时间戳的格式,传参是进行了判断,所以可以达到不同形式的显示。strtotime() 函数也很巧妙,牛哇牛哇

来源:https://blog.csdn.net/weixin_47736740/article/details/128562311

0
投稿

猜你喜欢

  • reader.html<html><head><meta  http-equiv=&quo
  • MySQL 一级防范检查列表以下是加固你的 Mysql 服务器安全所要做的工作的重要参考:Securing MySQL: step-by-s
  • Dreamweaver中一直变色的超级链接,css+javascript实现超级链接变色,当鼠标移动到链接上时,链接的颜色不停闪烁变色。&l
  • 实现一个AuditLog的功能,是B/S结构专案。 每个用户可以登录系统,在程序中操作数据(添加,更新和删除)需要实现记录操作跟踪。是谁添加
  • 客户端程序编写免不了经常接触XMLHttpRequest对象。微软的XHR实现的progid又是一串一串的。 烦人。抽一个中午时间,找了找资
  • 数据库连接字符串的常见问题和解决方法:基本知识1:SQL Server数据库的身份验证方式,分windows验证,SQL Server验证两
  • 本文给出了几个表单常用的js验证函数,有检查、\等特殊字符的,有检查是否含有空格,检查是否为Email 地址,也有检查是否是小数或负数的,检
  • 有时需要获取远程网站的某些信息,而服务器又限制了GET方式,只能通过POST数据提交,这个时候我们可以通过asp来实现模拟提交post数据,
  •   怎么增大MySQL数据库连接数,MYSQL数据库安装完成后,默认连接数是100,流量稍微大一点的论坛或网站这个连接数是不够哟用
  • 前面也讲过一次phar文件上传的东西,但是那都是过滤比较低,仅仅过滤了后缀。知道今天看到了一篇好的文章如果过滤了phar这个伪造协议的话,那
  • 突然想到写这个话题,是基于最近在设计产品界面时,年糕一直在给我们灌输设计的品牌概念以及文化内涵要求而产生的,因之前在界面设计中也遇到一些困惑
  • 其实就是利用文件“global.asa”!许多ASP编程新手都想知道这东西是什么?事实上,global.asa就是一个事件驱动程序,其中共包
  • 用DIV+CSS可以作出很多不同形状的角形;以下我只写了几个;CSS没有优化;是为了让大家看得更清一些;以下是一些小三角的形状:这是第一个小
  • 好久没有写ASP代码了,今天在做一个简单的留言本时,出现了一下错误:Microsoft Office Access Database Eng
  • b 和 i 标签在现在的 Web 标准潜规则中是不推荐使用,甚至是反对使用的,因为认为他们只是“表现”粗体和斜体,而没有任何“语义”。更多的
  • 最近在做搜索设计时,发现了两个容易纠结的小问题,在这里谈谈自己的一些分析。问题一:提交的关键字是哪个?凡客的这个例子中,搜索建议“时尚斜拉链
  • 在日常的工作中,保护数据免受未授权用户的侵犯是系统管理员特别关心的问题。如果你目前用的是MySQL,就可以使用一些方便的功能来保护系统,来大
  • 可以输入的下拉菜单,不错的一个效果,相信asp之家给大家收集的这个代码会有不少人需要!<html> <head> &
  • 我设了两个SESSION:SESSION(A1),SESSION(A2),然后我现在想结束其中一个SESSION(如:ESEEION(A1)
  • 四年前写的一个内容管理系统,应用在公司内部网上,昨天DBA说其中的SQL语句未使用参数化的调用,导致服务器负担加重,资源占用大。并列出了几个
手机版 网络编程 asp之家 www.aspxhome.com