PHP常用时间戳处理

合集下载

php 计算两个时间字符串的差值函数

php 计算两个时间字符串的差值函数

php 计算两个时间字符串的差值函数在日常开发中,经常会遇到计算时间差的需求。

比如,我们可能需要计算某个任务的执行时间,或者计算两个事件之间的时间间隔。

使用php可以很方便地实现这些功能。

在php中,我们可以使用strtotime()函数将时间字符串转换为时间戳。

时间戳表示从1970年1月1日00:00:00以来的秒数。

通过将时间字符串转换为时间戳,我们可以对时间进行各种操作,比如计算时间差。

下面是一个示例函数,用于计算两个时间字符串之间的差值:```phpfunction calculateTimeDifference($time1, $time2) {// 将时间字符串转换为时间戳$timestamp1 = strtotime($time1);$timestamp2 = strtotime($time2);// 计算时间差$difference = abs($timestamp2 - $timestamp1);// 将时间差转换为天、小时、分钟和秒$days = floor($difference / (60 * 60 * 24));$hours = floor(($difference - $days * 60 * 60 * 24) / (60 *60));$minutes = floor(($difference - $days * 60 * 60 * 24 - $hours * 60 * 60) / 60);$seconds = $difference - $days * 60 * 60 * 24 - $hours * 60 * 60 - $minutes * 60;// 返回时间差return "$days 天 $hours 小时 $minutes 分钟 $seconds 秒";}```上述函数接受两个时间字符串作为参数,然后使用strtotime()函数将这两个时间字符串转换为时间戳。

PHP--时间格式处理

PHP--时间格式处理

PHP--时间格式处理Ymd格式转Y-m-d或转成时间戳将Ymd格式如19930811转成1993-08-11格式date('Y-m-d',strtotime('19930811')将Ymd格式如19930811转成时间戳格式(注意:直接转会少⼀个⼩时即60*60秒),所以要加上3600 strtotime('19930811')+3600/*** 获取统计时间* @param $type* 1 上⽉* 2 本⽉* 3 近15天* 4 近30天* @return array*/function getDateInfo($type){$data = array(array('firstday' => date('Ym01', strtotime('-1 month')),'lastday' => date('Ymt', strtotime('-1 month')),),array('firstday' => date('Ym01', strtotime(date("Y-m-d"))),'lastday' => date('Ymd', strtotime((date('Ym01', strtotime(date("Y-m-d")))) . " +1 month -1 day")), ),array('firstday' => date('Ymd', strtotime("-15 day")),'lastday' => date('Ymd', strtotime('-1 day')),),array('firstday' => date('Ymd', strtotime("-30 day")),'lastday' => date('Ymd', strtotime('-1 day')),),);return is_null($type) ? $data : $data[$type-1];}//设置中国时区date_default_timezone_set('PRC');//今天的时间搓$today_start = strtotime(date('Y-m-d',time()).' 0:0:0');$today_end = strtotime(date('Y-m-d',time()).' 23:59:59');//昨天的时间戳$yesterday_start = strtotime('-1 day'.' 0:0:0');$yesterday_end = strtotime('-1 day'.' 23:59:59');//查看上个⽉⽇期$first_day_of_month = date('Y-m',time()).'-01 00:00:01';$t = strtotime($first_day_of_month);date('Y-m',$t);date('Y-m',strtotime('- 1 month',$t));date('Y-m',strtotime('- 2 month',$t));//获取时间戳⽅法⼀:$yesterday_s = mktime(0,0,0,date('m'),date('d')-1,date('Y'));//昨天开始的时间搓$yesterday_e = mktime(23,59,59,date('m'),date('d')-1,date('Y'));//昨天结束的时间搓$tenday_s = mktime(0,0,0,date('m'),date('d')-10,date('Y'));//10天前开始的时间戳$tenday_e = mktime(23,59,59,date('m'),date('d')-10,date('Y'));//10天前结束的时间戳//⽅法⼆:$yesterday_s = strtotime(date("Y-m-d",strtotime("-10 day")).' 0:0:0'); //10天前开始的时间戳$yesterday_e = strtotime(date("Y-m-d",strtotime("-10 day")).' 23:59:59');//10天前结束的时间戳//⽅法三:$day = $_REQUEST['day'];//需要统计的⽇期$time_s = strtotime($day.' 0:0:0');$time_e = strtotime($day.' 23:59:59');//获取本周⼀的时间戳strtotime(date("Y-m-d",strtotime("-1 week Monday")));1. //获取今⽇开始时间戳和结束时间戳2. $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));3. $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;4.5. //获取昨⽇起始时间戳和结束时间戳6. $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));7. $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;8.9. //获取本周起始时间戳和结束时间戳10. $beginThisweek = mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y'));11. $endThisweek=time();12.13. //获取上周起始时间戳和结束时间戳14. $beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));15. $endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));16.17. //获取本⽉起始时间戳和结束时间戳18. $beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));19. $endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));20.21. //上个⽉的起始时间:22. $begin_time = strtotime(date('Y-m-01 00:00:00',strtotime('-1 month')));23. $end_time = strtotime(date("Y-m-d 23:59:59", strtotime(-date('d').'day')));24.25. $begin_year = strtotime(date("Y",time())."-1"."-1"); //本年开始26. $end_year = strtotime(date("Y",time())."-12"."-31"); //本年结束27.28. //现在的时间到第⼆天凌晨相差的时间戳29. $time = (strtotime(date('Y-m-d'))+3600*24) - time() ;。

Thinkphp前端视图输出日期时间戳自动转换为时间格式化年月日

Thinkphp前端视图输出日期时间戳自动转换为时间格式化年月日

Thinkphp前端视图输出⽇期时间戳⾃动转换为时间格式化年⽉⽇需求:实现⽂章创建按年,⽉,⽇归类,并如图格式显⽰。

2018 年11 ⽉( 1 篇⽂章 )24⽇: 你⾛了真好,不然总担⼼你要⾛(130)05 ⽉( 1 篇⽂章 )12⽇: 后来的我们(90)03 ⽉( 2 篇⽂章 )31⽇: 年少不懂《还珠3》,看懂已是⽽⽴年(44)12⽇: 要么孤独,要么庸俗(60)数据库字段 article_createtime 格式为 timestamp前端代码<h3 class="al_year">{$article_res.article_createtime|strtotime|date="Y年",###}</h3><ul class="al_mon_list"><li><span class="al_mon">{$article_res.article_createtime|strtotime|date="m⽉",###} <em>、</em></span><ul class="al_post_list"><li>{$article_res.article_createtime|strtotime|date="d⽇",###}:<a href="https:///6643">{$article_res.article_title}</a><em>(130)</em></li></ul></li></ul>解释下:strtotime()把字符串转化为整数时间date(format, timestamp)把时间戳格式化为更易读的⽇期和时间百度上搜索有的答案是:{$article_res.article_createtime|date="y-m-d",###},经测试在timestamp格式下会得到⼀个异常,但是在int格式正常显⽰有的习惯把⽇期⽤int格式保存,则{$article_res.article_createtime|date="y-m-d",###}正常显⽰,但{$article_res.article_createtime|strtotime|date="y-m-d",###}会得到1970-01-01.需求中仅分别需要显⽰年,或⽉,或⽇,可以通过修改格式内容来获得需要的数据date="y",###date="m",###date="d",###date="y年",### :2019年date="m⽉",###:11⽉date="d⽇",###: 2⽇总结如果数据库是 timestamp 格式:{$article_res.article_createtime|strtotime|date="y-m-d",###}如果数据库是 int 格式:{$article_res.article_createtime|date="y-m-d",###} {$article_res.article_createtime|date="y-m-d",###} {$article_res.article_createtime|strtotime|date="y-m-d",###}timestamp 异常:A non well formed numeric value encountered正确值int正确值错误值:1970-01-01。

php的strtodate函数

php的strtodate函数

php的strtodate函数strtodate函数是PHP中常用的日期格式转换函数之一。

它的作用是将一个字符串转换为日期格式。

在PHP中,日期和时间的处理是非常重要的。

在开发中,我们经常需要对日期进行格式化、比较、计算等操作。

而strtodate函数就是用来将一个字符串按照指定的格式转换为日期格式的。

使用strtodate函数,我们可以将一个字符串表示的日期转换为一个日期类型的变量。

这样就可以方便地对日期进行各种操作和处理。

strtodate函数的语法如下:strtotime(string $time [, int $now = time() ]) : int参数$time是一个表示日期的字符串,$now是一个可选的参数,表示参照时间,默认为当前时间。

下面我们来看一个例子:```$dateString = "2022-01-01";$date = strtotime($dateString);echo date("Y年m月d日", $date);```上面的代码中,我们将一个字符串"2022-01-01"使用strtodate函数转换为一个日期类型的变量$date。

然后使用date函数将日期格式化为"Y年m月d日"的格式,并输出结果。

strtodate函数支持的日期格式非常丰富,可以包括年、月、日、时、分、秒等各种单位。

具体的格式化选项可以参考PHP官方文档。

除了日期格式的转换,strtodate函数还可以进行日期的计算。

比如,我们可以通过给定一个时间字符串和一个时间间隔,来计算出未来或过去的日期。

下面是一个例子:```$dateString = "2022-01-01";$interval = "+1 week";$date = strtotime($dateString . " " . $interval);echo date("Y年m月d日", $date);```上面的代码中,我们给定了一个时间字符串"2022-01-01"和一个时间间隔"+1 week",然后使用strtotime函数计算出未来一周的日期,并使用date函数进行格式化输出。

php上传图片之时间戳命名(保存路径)

php上传图片之时间戳命名(保存路径)

php上传图⽚之时间戳命名(保存路径)html代码:<div id="images" style="width:250px;height:120px;background:#fff;border:1px solid #ccc;"><h2><strong>图⽚导⼊</strong></h2><form enctype="multipart/form-data" action="./includer/importimg.inc.php?action=img" method="post" name="imge"><input type="hidden" name="MAX_FILE_SIZE" value="100000000" /><input value="导⼊⽂件" type="file" name="img" id="file"/><br/><br/><input type="submit" id="imgbut" class="buttons" value="上传图⽚" /></form></div>php代码:<?php/***Mwbe Version1.0*-----------------------------------------------*Copy 2013-2014 ylt*Web: communicate*-----------------------------------------------*Author: tao *Data: 2014-7-22*/header("Content-Type:text/html;charset=utf-8");//step 1 使⽤$_FILES['pic']["error"] 检查错误if(isset($_GET["action"])=="img"){if($_FILES["img"]["error"] > 0){switch($_FILES["img"]["error"]) {case 1:echo "<script type='text/javascript'>alert('上传的⽂件超过了 php.ini 中 upload_max_filesize 选项限制的值<br>');history.back();</script>"; break;case 2:echo "<script type='text/javascript'>alert('上传⽂件的⼤⼩超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值');history.back();</script>"; break;case 3:echo "<script type='text/javascript'>alert('⽂件只有部分被上传');history.back();</script>";break;case 4:echo "<script type='text/javascript'>alert('没有⽂件被上传');history.back();</script>";break;default:echo "<script type='text/javascript'>alert('末知错误');history.back();</script>";}exit;}$maxsize=2000000; //50k//step 2 使⽤$_FILES["pic"]["size"] 限制⼤⼩单位字节 2M=2000000if($_FILES["img"]["size"] > $maxsize ) {echo "<script type='text/javascript'>alert('上传的⽂件太⼤,不能超过{$maxsize}字节');history.back();</script>";exit;}//step 3 使⽤$_FILES["pic"]["type"]或是⽂件的扩展名限制类型 MIME image/gif image/png gif png jpg/* list($dl, $xl) = explode("/", $_FILES["pic"]["type"]);if($dl!="image"){echo "请上传⼀个图⽚,不充许其它类型⽂件";exit;}*/$allowtype=array("png", "gif", "jpg", "jpeg");$arr=explode(".", $_FILES["img"]["name"]);$hz=$arr[count($arr)-1];if(!in_array($hz, $allowtype)){echo "<script type='text/javascript'>alert('这是不允许的类型');history.back();</script>";exit;}//step 4 将让传后的⽂件名改名$filepath="../imgweb/";$fileimgweb="imgweb/";//为了符合UBB的路径$randname=date("Y").date("m").date("d").date("H").date("i").date("s").rand(100, 999).".".$hz;//将临时位置的⽂件移动到指定的⽬录上即可if(is_uploaded_file($_FILES["img"]["tmp_name"])){if(move_uploaded_file($_FILES["img"]["tmp_name"],$filepath.$randname)){echo "<script type='text/javascript'>history.back();</script>";session_start();$_SESSION['images'] = $fileimgweb.$randname;}else{echo "<script type='text/javascript'>alert('上传失败');history.back();</script>";}}else{echo"<script type='text/javascript'>alert('不是⼀个上传⽂件');history.back();</script>"; }}>。

php中strtotime参数

php中strtotime参数

php中strtotime参数在PHP 中,`strtotime` 函数用于将人类可读的日期时间字符串转换为Unix 时间戳。

`strtotime` 函数接受一个表示日期时间的字符串作为参数,并返回对应的Unix 时间戳。

`strtotime` 函数的参数可以是以下几种形式:1. 日期时间字符串:```php$timestamp = strtotime("2023-01-01 12:00:00");```在这个例子中,`strtotime` 将"2023-01-01 12:00:00" 转换为对应的Unix 时间戳。

2. 相对日期时间字符串:```php$timestamp = strtotime("+1 day");```这将返回当前时间24 小时后的Unix 时间戳。

```php$timestamp = strtotime("next Sunday");```这将返回下一个星期天的Unix 时间戳。

相对日期时间字符串可以包含诸如"now"、"yesterday"、"tomorrow"、"next week" 等表达式。

3. 带有时区信息的日期时间字符串:```php$timestamp = strtotime("2023-01-01 12:00:00 UTC");```在这个例子中,字符串包含了时区信息"UTC"。

4. 日期时间字符串和现有时间戳的组合:```php$timestamp = strtotime("2023-01-01 12:00:00", $existingTimestamp);```这将使用现有的时间戳作为基准,将"2023-01-01 12:00:00" 加到该时间戳上。

thinkphp datediff用法

thinkphp datediff用法

ThinkPHP框架中的datediff用法详解一、前言ThinkPHP是一个开源的PHP框架,其灵活易用的特点使得它在开发领域广受欢迎。

其中,datediff函数是ThinkPHP中常用的一个日期处理函数,它可以用来计算两个日期之间的差值。

二、datediff基本用法在ThinkPHP中,datediff函数的基本语法如下:```$diff = D('Common')->dateDiff($start_date, $end_date);```在这个例子中,$start_date和$end_date分别是开始日期和结束日期,它们可以是时间戳,也可以是日期格式的字符串。

datediff函数会返回这两个日期之间的差值,单位是天。

三、datediff进阶用法1. 时间单位的设置:默认情况下,datediff函数返回的是天数,如果你需要其他的时间单位,可以通过修改第二个参数来实现。

例如,如果你想得到小时数,可以这样写:```$diff = D('Common')->dateDiff($start_date, $end_date, 'h');```2. 日期格式的设置:如果你的日期不是标准的日期格式,你可以通过strtotime 函数将其转换为时间戳,然后再传递给datediff函数。

例如:```$start_date = strtotime('2021-01-01');$end_date = strtotime('2021-01-31');$diff = D('Common')->dateDiff($start_date, $end_date);```四、注意事项1. 请确保你的日期格式是正确的,否则datediff函数可能会返回错误的结果。

2. datediff函数只能比较日期,不能比较时间。

PHP时间戳函数(几分钟、几小时前、几天前,今天零点等)

PHP时间戳函数(几分钟、几小时前、几天前,今天零点等)

PHP时间戳函数(⼏分钟、⼏⼩时前、⼏天前,今天零点等)时间戳是我们在时间⽇期对⽐时常⽤到⼀个⼩功能,下⾯我先来给各位介绍strtotime时间戳转换的⼀些⽅法与利⽤它来做⼀个⽇期格式化的⼏分钟、⼏⼩时前、⼏天前的⼀个实例。

1.PHP时间戳函数将⽇期转化为unix时间戳世界末⽇时间戳 PHPecho "世界末⽇时间戳为:".strtotime("2012-12-21")2.将时间戳转化为系统时间date('Y-m-d H:i:s',"1228348800");(1)获取当天零点时间戳$timetoday = strtotime(date("Y-m-d",time()));(2)获取明天零点时间戳$tomorrow = $timetoday + 3600*24;3.PHP时间戳函数获取英⽂⽂本⽇期时间⽰例如下:便于⽐较,使⽤date将当时间戳与指定时间戳转换成系统时间(1)打印明天此时的时间戳strtotime(”+1 day”)//当前时间echo date("Y-m-d H:i:s",time());//明天此时时间echo date("Y-m-d H:i:s",strtotime("+1 day"));(2)打印昨天此时的时间戳strtotime(”-1 day”)//当前时间echo date("Y-m-d H:i:s",time()) ;//指定时间echo date("Y-m-d H:i:s",strtotime("-1 day"));(3)打印下个星期此时的时间戳strtotime("+1 week")//当前时间echo date("Y-m-d H:i:s",time());//下星期时间echo date("Y-m-d H:i:s",strtotime("+1 week"));(4)打印上个星期此时的时间戳strtotime("-1 week")//当前时间echo date("Y-m-d H:i:s",time());//上个星期此时时间echo date("Y-m-d H:i:s",strtotime("-1 week"));(5)打印指定下星期⼏的时间戳strtotime("next Thursday")//当前时间echo date("Y-m-d H:i:s",time());//下星期⼏时间echo date("Y-m-d H:i:s",strtotime("next Thursday"));(6)打印指定上星期⼏的时间戳strtotime(”last Thursday”)//当前时间echo date("Y-m-d H:i:s",time());//指定时间echo date("Y-m-d H:i:s",strtotime("last Thursday"));以上PHP时间戳函数⽰例可知,strtotime能将任何英⽂⽂本的⽇期时间描述解析为Unix时间戳,我们结合mktime()或date()格式化⽇期时间获取指定的时间戳,实现所需要的⽇期时间话说看到别⼈写的这么⼀个函数,测试了⼀下,有些⼩⽑病:对于跨年⽇期,没有显⽰年份。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

//今日开始和结束的时间戳
$start = mktime(0 0 0 date('m') date('d') date('Y'));
$end = mktime(23 59 59 date('m') date('d') date('Y'));
//昨日开始和结束的时间戳
$start = mktime(0 0 0 date('m') date('d') - 1 date('Y'));
$end = mktime(23 59 59 date('m') date('d') - 1 date('Y'));
//本周开始和结束的时间戳
$start = strtotime(date('Y-m-d' strtotime("this week Monday" time())));
$end = strtotime(date('Y-m-d' strtotime("this week Sunday" time()))) + 24 * 3600 - 1;
//上周开始和结束的时间戳
$start = strtotime(date('Y-m-d' strtotime("last week Monday" time())));
$end = strtotime(date('Y-m-d' strtotime("last week Sunday" time()))) + 24 * 3600 - 1; //本月开始和结束的时间戳
$start = mktime(0 0 0 date('m') 1 date('Y'));
$end = mktime(23 59 59 date('m') date('t') date('Y'));
//上个月开始和结束的时间戳
$start = mktime(0 0 0 date('m') - 1 1 date('Y'));
$end = mktime(23 59 59 date('m') - 1 date('t' $start) date('Y'));
//今年开始和结束的时间戳
$start = mktime(0 0 0 1 1 date('Y'));
$end = mktime(23 59 59 12 31 date('Y'));
//去年开始和结束的时间戳
$start = mktime(0 0 0 1 1 date('Y') - 1);
$end = mktime(23 59 59 12 31 date('Y') - 1);
//获取两个时间戳相差几天
function day_to_day($start$end){
$r = $end-$start;
if($r<0){
$day = -($r/86400);
$day = ceil($day);
}else{
$day = ceil($r/86400);
}
return $day;
}
//本周一
echo date('Y-m-d' (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)); //w为星期几的数字形式这里0为周日
//本周日
echo date('Y-m-d' (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600)); //同样使用w以现在与周日相关天数算
//上周一
echo date('Y-m-d' strtotime('-1 monday' time())); //无论今天几号-1 monday为上一个有效周未
//上周日
echo date('Y-m-d' strtotime('-1 sunday' time())); //上一个有效周日同样适用于其它星期
//本月一日
echo date('Y-m-d' strtotime(date('Y-m' time()) . '-01 00:00:00')); //直接以strtotime生成
//本月最后一日
echo date('Y-m-d' strtotime(date('Y-m' time()) . '-' . date('t' time()) . ' 00:00:00')); //t为当月天数28至31天
//上月一日
echo date('Y-m-d' strtotime('-1 month' strtotime(date('Y-m' time()) . '-01 00:00:00'))); //本月一日直接strtotime上减一个月
//上月最后一日
echo date('Y-m-d' strtotime(date('Y-m' time()) . '-01 00:00:00') - 86400); //本月一日减一天即是上月最后一日
//人性化时间显示
function formatTime($sTime $formt = 'Y-m-d') {
if (!$sTime) {
return '';
}
//sTime=源时间,cTime=当前时间,dTime=时间差
$cTime = time();
$dTime = $cTime - $sTime;
$dDay = intval(date('z'$cTime)) - intval(date('z'$sTime));
$dYear = intval(date('Y'$cTime)) - intval(date('Y'$sTime));
//n秒前,n分钟前,n小时前,日期
if ($dTime < 60 ) {
if ($dTime < 10) {
return '刚刚';
} else {
return intval(floor($dTime / 10) * 10).'秒前';
}
} elseif ($dTime < 3600 ) {
return intval($dTime/60).'分钟前';
} elseif( $dTime >= 3600 && $dDay == 0 ){
return intval($dTime/3600).'小时前';
} elseif( $dDay > 0 && $dDay<=7 ){
return intval($dDay).'天前';
} elseif( $dDay > 7 && $dDay <= 30 ){
return intval($dDay/7).'周前';
} elseif( $dDay > 30 ){
return intval($dDay/30).'个月前';
} elseif ($dYear==0) {
return date('m月d日' $sTime);
} else {
return date($formt $sTime); }
}。

相关文档
最新文档