将日期和时间转变成UNIX时间戳
函数mktime()
<?php
echo date("Y-m-d", mktime(0, 0, 0, 12, 36, 2008))."\n"; //日期超过31天,计算后输出 2009-01-05
echo date("Y-m-d", mktime(0, 0, 0, 14, 1, 2010))."\n"; //月份超过12月,计算后输出 2011-02-01
echo date("Y-m-d", mktime(0, 0, 0, 1, 1, 2012))."\n"; //没有问题的转变,输出结果 2012-01-01
echo date("Y-m-d", mktime(0, 0, 0, 1, 1, 99))."\n"; //会将99年转变为1999年, 1990-01-01
?>
函数strtotime()
<?php
echo date("Y-m-d", strtotime("now")); //输出: 2012-04-05
echo date("Y-m-d", strtotime("8 may 2012")); //输出: 2012-05-08
echo date("Y-m-d", strtotime("+1 day")); //输出: 2012-04-06
echo date("Y-m-d", strtotime("last monday")); //输出: 2012-04-02
?>
使用函数strtotime()编写一个纪念日的倒计时程序
<?php
$now = strtotime("now"); //当前时间
$endtime = strtotime("2014-08-18 08:08:08"); //设定毕业时间,转成时间戳
$second = $endtime - $now; //获取毕业时间到现在时间的时间戳(秒数)
$year = floor($second/3600/24/365); //从这个时间戳中换算出年头数
$temp = $second - $year*365*24*3600; //从时间戳中去掉整年的秒数,就剩下月份的秒数
$month = floor($temp/3600/24/30); //从这个时间戳中换算出月数
$temp = $temp - $month*30*24*3600; //从时间戳中去掉整月的秒数,就剩下天的秒数
$day = floor($temp/3600/24); //从这个时间戳中换算出剩余的天数
$temp = $temp - $day*3600*24; //从时间戳中去掉整天的秒数,就剩下小时的秒数
$hour = floor($temp/3600); //从这个时间戳中换算出剩余的小时数
$temp = $temp - $hour*3600; //从时间戳中去掉整小时的秒数,就剩下分的秒数
$minute = floor($temp/60); //从这个时间戳中换算出剩余的分数
$second1 = $temp - $minute*60; //最后就只有剩余的秒数了
echo "距离培训毕业还有{$year}年{$month}月{$day}天{$hour}小时{$minute}分{$second1}秒。";
?>
|