《php时间函数汇总》要点:
本文介绍了php时间函数汇总,希望对您有用。如果有疑问,可以联系我们。
date_default_timezone_set('Asia/Shanghai');
function run_time(){
list($msec,$sec) = explode(" ",microtime());
return ((float)$msec + (float)$sec);
}
$start_time = run_time();
$time = time();
echo "time(): {$time}";
$formats = array('U','r','c','l , F jS , Y , g:i A', 'H:i:s D d M y', 'm/j/y/ g:i:s a O (T)', 'Y-m-d H:i:s');
foreach($formats as $format){
echo "$format: ".date($format)."\n";
}
$getdate_array = getdate($time);
echo "getdate(): ";
print_r($getdate_array);
echo "";
echo '';
$dateArray = array(
"now", "today", "tomorrow", "yesterday",
"Thursday", "this Thursday", "last Thursday",
"+2 hours", "-1 month", "+10 minutes", "30 seconds",
"+2 years -1 month", "next week", "last month",
"last year", "2 week ago", "next Friday"
);
foreach($dateArray as $mydate){
echo "\n";
}
echo '$mydate: ".date('Y-m-d H:i:s', strtotime($mydate))."';
echo '';
setlocale(LC_TIME, "zhs");
echo strftime("Month: %B ");
echo strftime("Day: %A ");
echo '';
echo "mktime函数返回的时间戳:".mktime()."";
echo "当前的日期为:".date("Y-m-d",mktime())."";
echo "当前的时间为:".date("H:i:s",mktime());
echo '';
//time函数
echo "time函数返回的时间戳:".time().'';
echo '当前日期为:'.date('Y-m-d H:i:s')."";
echo "当前的时间为:".date("H:i:s");
echo '';
$timeNow = strtotime(date("Y-m-d H:i:s"));
$time2 = strtotime("2012-12-21 8:10:10");
$sub1 = ceil(($time2 - $timeNow)/3600);
$sub2 = ceil(($time2 - $timeNow)/86400);
echo "离世界末日还有".$sub2."天";
echo "也便是".$sub1."小时";
echo '';
echo 'microtime()函数值:'.microtime();
echo '';
echo '日期转时间戳:2011-2-27:';
echo strtotime("2011-2");
echo '';
echo '时间戳转日期:1298736000:';
echo date("Y-m-d","1298736000");
echo '';
//昨天
$day_s = mktime(0,0,0,date("m", strtotime("-1 day")), date("d",strtotime("-1 day")), date("Y",strtotime("-1 day")));
$day_e = mktime(23,59,59,date("m", strtotime("-1 day")), date("d",strtotime("-1 day")), date("Y",strtotime("-1 day")));
echo '昨天';
echo date('Y-m-d H:i:s', $day_s).'';
echo date('Y-m-d H:i:s', $day_e).'';
$year = date( 'Y' );
$month = date('m');
$day = date('d');
//本周
echo '本周';
echo date('Y-m-d H:i:s', mktime(0, 0, 0, $month, $day - date('N'), $year)).'';
echo date('Y-m-d H:i:s', mktime(23, 59, 59, $month,$day - date('N') + 6, $year)).'';
//上周
echo '上周';
echo date('Y-m-d H:i:s', mktime(0, 0, 0, $month, $day - date('N') - 7, $year)).'';
echo date('Y-m-d H:i:s', mktime(23, 59, 59, $month, $day - date('N') -1 , $year)).'';
//本月
echo '本月';
echo date("Y-m-d H:i:s", mktime(0, 0, 0, date('m'), 1, date('y'))).'';
echo date("Y-m-d H:i:s", mktime(23, 59, 59, date('m')+1, 0, date('y'))).'';
//上月
echo '上月';
echo date("Y-m-d H:i:s", mktime(0, 0, 0, date('m')-1, 1, date('y'))).'
';
echo date("Y-m-d H:i:s", mktime(23, 59, 59, date('m'), 0, date('y'))).'';
$end_time = run_time();
echo '该网页的运行时间为:';
echo ($end_time - $start_time);
echo '';
上面格局可能太乱补上几张图片:
维易PHP培训学院每天教你实战技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培养人才。
转载请注明本页网址:
http://www.vephp.com/jiaocheng/6689.html