PHP格式化时间

本文介绍了如何使用PHP和Smarty模板引擎进行日期和时间的格式化输出。通过具体实例展示了strtotime函数的应用,以及如何在Smarty模板中利用date_format过滤器显示时间戳。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

php中格式化输出日期和时间可用:date('Y-m-d H:i:s',时间戳); 的形式输出,对应的是“年-月-日 时:分:秒”。

而在smarty模板中,如$time是php文件中assign过来的时间戳,在模板文件中写法为:

<{$time|date_format:'%Y-%m-%d %H:%M:%S'}> ,同样对应的输出格式为:“年-月-日 时:分:秒”。

 

php文件:

Php代码  收藏代码
  1. <?php  
  2.   
  3.   //导入自定义smarty操作类SmartyInit.php  
  4.   include_once('class/SmartyInit.php');  
  5.   $smarty = new SmartyInit();  
  6.     
  7.   //设置默认时区为上海  
  8.   date_default_timezone_set('Asia/Shanghai');  
  9.   //输出echo strtotime('now'),结果如:1245763672  
  10.   //可知strtotime('now')返回的是时间戳  
  11.   
  12.   //也可是从数据库得到的时间戳  
  13.   $time = time();  
  14.   
  15.   echo 'php格式化输出:<br />';  
  16.   echo '昨天:'.date('Y-m-d H:i:s'strtotime('-1 day')).'<br />';  
  17.   //date('Y-m-d H:i:s'),不写第二个参数,默认为当前时间  
  18.   //也可写为:date('Y-m-d H:i:s', strtotime('now'))  
  19.   echo '今天:'.date('Y-m-d H:i:s').'<br />';  
  20.   echo '明天:'.date('Y-m-d H:i:s'strtotime('1 day')).'<br />';  
  21.   echo '赋值时间戳:'.date('Y-m-d H:i:s'$time).'<br />';  
  22.   
  23.   //strtotime('today')只输出当天日期,  
  24.   //strtotime('today 00:00:00')可输出时间  
  25.   $smarty->assign('yesterday'strtotime('yesterday'));  
  26.   $smarty->assign('today'strtotime('today 20:15:04'));  
  27.   $smarty->assign('tomorrow'strtotime('tomorrow'));  
  28.     
  29.     
  30.   $smarty->assign('yesterday1'strtotime('-1 day'));  
  31.   //等同$smarty->assign('today1', strtotime('0 day'));  
  32.   $smarty->assign('today1'strtotime('now'));  
  33.   $smarty->assign('tomorrow1'strtotime('1 day'));  
  34.   $smarty->assign('time'$time);  
  35.     
  36.   $smarty->display('index.html');  

 模板文件(html):

Html代码  收藏代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>smarty测试</title>  
  6. </head>  
  7.   
  8. <body>  
  9.   
  10. <p>smarty模板输出:<br />  
  11. 昨天:<{$yesterday|date_format:'%Y-%m-%d %H:%M:%S'}>  
  12. <br />  
  13. 今天:<{$today|date_format:'%Y-%m-%d %H:%M:%S'}>  
  14. <br />  
  15. 明天:<{$tomorrow|date_format:'%Y-%m-%d %H:%M:%S'}>  
  16. </p>  
  17. <p>  
  18. 昨天:<{$yesterday1|date_format:'%Y-%m-%d %H:%M:%S'}>  
  19. <br />  
  20. 今天:<{$today1|date_format:'%Y-%m-%d %H:%M:%S'}>  
  21. <br />  
  22. 明天:<{$tomorrow1|date_format:'%Y-%m-%d %H:%M:%S'}>  
  23. <br />  
  24. 赋值时间戳:<{$time|date_format:'%Y-%m-%d %H:%M:%S'}>  
  25. </p>  
  26.   
  27. smarty保留变量输出:<{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}>  
  28. </body>  
  29. </html>  
<!--EndFragment-->
### 如何在 PHP格式化两个时间戳之间的差异 在 PHP 中,可以通过计算两个时间戳的差值并将其转换为所需的格式来实现时间差的显示。以下是具体方法: #### 方法一:使用 `DateTime` 和 `DateInterval` PHP 提供了一个强大的类 `DateTime` 来处理日期和时间操作。通过创建两个 `DateTime` 对象表示不同的时间戳,并调用其内置函数 `diff()` 计算两者之间的差异。 ```php <?php // 创建 DateTime 对象 $datetime1 = new DateTime('@' . $startTimestamp); // 使用 Unix 时间戳初始化 $datetime2 = new DateTime('@' . $endTimestamp); // 获取时间间隔对象 $interval = $datetime1->diff($datetime2); // 输出格式化的差异 echo $interval->format('%y 年 %m 月 %d 日 %h 小时 %i 分钟 %s 秒'); ?> ``` 此代码会返回两个时间戳之间的时间差,单位可以精确到年、月、日、小时、分钟和秒[^1]。 #### 方法二:手动计算时间差 如果只需要简单的总秒数或其他自定义格式,可以直接减去两个时间戳并进行相应的换算。 ```php <?php $timeDifferenceInSeconds = abs($endTimestamp - $startTimestamp); $totalDays = floor($timeDifferenceInSeconds / (60 * 60 * 24)); $totalHours = ($totalDays * 24) + floor(($timeDifferenceInSeconds % (60 * 60 * 24)) / (60 * 60)); $totalMinutes = ($totalHours * 60) + floor((($timeDifferenceInSeconds % (60 * 60))) / 60); $totalSeconds = $timeDifferenceInSeconds % 60; echo "$totalDays days, $totalHours hours, $totalMinutes minutes, $totalSeconds seconds"; ?> ``` 这种方法允许更灵活地控制输出格式,并且适用于不需要完整的日期组件的情况[^3]。 #### 方法三:结合 GMT 偏移量调整时间差 当涉及到不同时区的时间戳时,可能还需要考虑 GMT 的偏移量。这可以通过设置默认时区或者直接读取时间戳中的偏移信息完成。 ```php <?php date_default_timezone_set('UTC'); // 设置默认时区为 UTC $startTimestampWithOffset = strtotime('+02:00', $startTimestamp); // 调整起始时间戳至指定时区 $endTimestampWithOffset = strtotime('+02:00', $endTimestamp); // 调整结束时间戳至相同时区 $timeDifferenceInSeconds = abs($endTimestampWithOffset - $startTimestampWithOffset); echo gmdate("H:i:s", $timeDifferenceInSeconds); // 格式化为 HH:mm:ss 形式的字符串 ?> ``` 这里利用了 `strtotime()` 函数加上特定的 GMT 差异来进行时间戳调整[^2]。 --- #### 总结 以上三种方式分别适合不同场景下的需求。对于复杂的应用程序开发而言,推荐优先采用第一种基于 `DateTime` 类的方法因为它不仅功能强大而且易于维护;而对于轻量化项目则可以选择第二种纯算法的方式减少依赖开销;最后第三种则是针对跨区域应用特别设计出来的解决方案确保准确性的同时兼顾性能表现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值