php时间有关基础处理

<?php

date_default_timezone_set('Asia/Shanghai');

//星期一:Monday(Mon.)
//星期二:Tuesday(Tues.)
//星期三:Wednesday(Wed.)
//星期四:Thursday(Thur./Thurs.)
//星期五:Friday(Fri.)
//星期六:Saturday(Sat.)
//星期日:Sunday(Sun.)
//last next +n -n  year month day hours seconds

//时间
echo '<br/>时间';
$time = time();
timetoecho($time);

timetoecho(strtotime("now"), 'strtotime("now")');
timetoecho(strtotime("10 September 2000"), 'strtotime("10 September 2000")');
timetoecho(strtotime("+1 day"), 'strtotime("+1 day")');
timetoecho(strtotime("+1 week"), 'strtotime("+1 week")');
timetoecho(strtotime("-1 week"), 'strtotime("-1 week")');
timetoecho(strtotime("+1 week 2 days 4 hours 2 seconds"), 'strtotime("+1 week 2 days 4 hours 2 seconds")');
timetoecho(strtotime("-1 week -2 days -4 hours -2 seconds"), 'strtotime("-1 week -2 days -4 hours -2 seconds")');
timetoecho(strtotime("next Thursday"), 'strtotime("next Thursday")');
timetoecho(strtotime("last Monday"), 'strtotime("last Monday")');
timetoecho(strtotime("+1 month"), 'strtotime("+1 month")');
timetoecho(strtotime("next month"), 'strtotime("next month")');
timetoecho(strtotime("last month"), 'strtotime("last month")');
timetoecho(strtotime("+1 year"), 'strtotime("+1 year")');
timetoecho(strtotime("last year +1 month"), 'strtotime("last year +1 month")');
timetoecho(strtotime("2022-12-30 21:21:58 last year +1 month"), 'strtotime("2022-12-30 21:21:58 last year +1 month")');
timetoecho(strtotime(date("Y-m-d H:i:s") . "-1 week last Sunday -2 day"), 'strtotime(上上周五date("Y-m-d H:i:s") . "-1 week last Sunday -2 day")');



//日期
echo '<br/>日期';
datetoecho(strtotime('2030-11-09 12:12:09'));

dmonth(strtotime('2030-11-09 12:12:09'), strtotime('2019-01-19 12:12:09'));

dday(strtotime('2030-11-09 12:12:09'), strtotime('2030-11-19 22:32:09'));

function timetoecho($time, $s = ''){
    
    echo '<br/>curtime: ' . date('Y-m-d H:i:s', time());
    
    echo '<br/>'.$s.' time: ' . $time;

    $date = date('Y-m-d H:i:s', $time);
    echo '<br/>'.$s.' date: ' . $date;

    $strtotime = strtotime($date);
    echo '<br/>'.$s.' strtotime: ' . $strtotime;
    
    echo '<br/><br/>';
}

function datetoecho($time, $s = ''){
    
    echo '<br/>date: ' . date('Y-m-d H:i:s', $time);

    echo '<br/>'.$s.' date("d", $time): ' . date("d", $time);
    echo '<br/>'.$s.' date("Y-n-j G:i:s", $time): ' . date('Y-n-j G:i:s', $time);
    echo '<br/>当月最后一天';
    echo '<br/>'.$s.' date("t", $time): ' . date('t', $time);
    echo '<br/>'.$s.' date("Y-m-t", $time): ' . date("Y-m-t", $time);
    echo '<br/>'.$s.' date("Y-m-d H:i:s", strtotime(date("Y-m-01 H:i:s", $time) . "+1 month -1 day")): ' . date("Y-m-d H:i:s", strtotime(date("Y-m-01 H:i:s", $time) . '+1 month -1 day'));
    
    echo '<br/><br/>';
}

function dmonth($time1, $time2, $s = ''){
    
    echo '<br/>date: ' . date('Y-m-d H:i:s', $time1);
    echo '<br/>date: ' . date('Y-m-d H:i:s', $time2);

    $y1 = (int)date("Y", $time2);
    $y2 = (int)date("Y", $time1);
    $m1 = (int)date("n", $time2);
    $m2 = (int)date("n", $time1);
    $y = abs($y1-$y2);
    $m = $y1>$y2?(12-$m2+$m1):($y1<$y2?(12-$m1+$m2):(abs($m1-$m2)));
    $y = $y>1?($y-1):0;

    echo '<br/>'.$s.' 相差月份 : ' . (($y*12)+$m);
    
    echo '<br/><br/>';
}

function dday($time1, $time2, $s = ''){
    
    echo '<br/>date: ' . date('Y-m-d H:i:s', $time1);
    echo '<br/>date: ' . date('Y-m-d H:i:s', $time2);
    
    $d = intval(abs($time1-$time2)/(60*60*24));

    echo '<br/>'.$s.' 相差天数 intval(abs($time1-$time2)/(60*60*24)): ' . $d;
    
    echo '<br/><br/>';
}

时间
curtime: 2019-03-22 17:27:33
time: 1553246853
date: 2019-03-22 17:27:33
strtotime: 1553246853


curtime: 2019-03-22 17:27:33
strtotime("now") time: 1553246853
strtotime("now") date: 2019-03-22 17:27:33
strtotime("now") strtotime: 1553246853


curtime: 2019-03-22 17:27:33
strtotime("10 September 2000") time: 968515200
strtotime("10 September 2000") date: 2000-09-10 00:00:00
strtotime("10 September 2000") strtotime: 968515200


curtime: 2019-03-22 17:27:33
strtotime("+1 day") time: 1553333253
strtotime("+1 day") date: 2019-03-23 17:27:33
strtotime("+1 day") strtotime: 1553333253


curtime: 2019-03-22 17:27:33
strtotime("+1 week") time: 1553851653
strtotime("+1 week") date: 2019-03-29 17:27:33
strtotime("+1 week") strtotime: 1553851653


curtime: 2019-03-22 17:27:33
strtotime("-1 week") time: 1552642053
strtotime("-1 week") date: 2019-03-15 17:27:33
strtotime("-1 week") strtotime: 1552642053


curtime: 2019-03-22 17:27:33
strtotime("+1 week 2 days 4 hours 2 seconds") time: 1554038855
strtotime("+1 week 2 days 4 hours 2 seconds") date: 2019-03-31 21:27:35
strtotime("+1 week 2 days 4 hours 2 seconds") strtotime: 1554038855


curtime: 2019-03-22 17:27:33
strtotime("-1 week -2 days -4 hours -2 seconds") time: 1552454851
strtotime("-1 week -2 days -4 hours -2 seconds") date: 2019-03-13 13:27:31
strtotime("-1 week -2 days -4 hours -2 seconds") strtotime: 1552454851


curtime: 2019-03-22 17:27:33
strtotime("next Thursday") time: 1553702400
strtotime("next Thursday") date: 2019-03-28 00:00:00
strtotime("next Thursday") strtotime: 1553702400


curtime: 2019-03-22 17:27:33
strtotime("last Monday") time: 1552838400
strtotime("last Monday") date: 2019-03-18 00:00:00
strtotime("last Monday") strtotime: 1552838400


curtime: 2019-03-22 17:27:33
strtotime("+1 month") time: 1555925253
strtotime("+1 month") date: 2019-04-22 17:27:33
strtotime("+1 month") strtotime: 1555925253


curtime: 2019-03-22 17:27:33
strtotime("next month") time: 1555925253
strtotime("next month") date: 2019-04-22 17:27:33
strtotime("next month") strtotime: 1555925253


curtime: 2019-03-22 17:27:33
strtotime("last month") time: 1550827653
strtotime("last month") date: 2019-02-22 17:27:33
strtotime("last month") strtotime: 1550827653


curtime: 2019-03-22 17:27:33
strtotime("+1 year") time: 1584869253
strtotime("+1 year") date: 2020-03-22 17:27:33
strtotime("+1 year") strtotime: 1584869253


curtime: 2019-03-22 17:27:33
strtotime("last year +1 month") time: 1524389253
strtotime("last year +1 month") date: 2018-04-22 17:27:33
strtotime("last year +1 month") strtotime: 1524389253


curtime: 2019-03-22 17:27:33
strtotime("2022-12-30 21:21:58 last year +1 month") time: 1643548918
strtotime("2022-12-30 21:21:58 last year +1 month") date: 2022-01-30 21:21:58
strtotime("2022-12-30 21:21:58 last year +1 month") strtotime: 1643548918


curtime: 2019-03-22 17:27:33
strtotime(上上周五date("Y-m-d H:i:s") . "-1 week last Sunday -2 day") time: 1551974400
strtotime(上上周五date("Y-m-d H:i:s") . "-1 week last Sunday -2 day") date: 2019-03-08 00:00:00
strtotime(上上周五date("Y-m-d H:i:s") . "-1 week last Sunday -2 day") strtotime: 1551974400


日期
date: 2030-11-09 12:12:09
date("d", $time): 09
date("Y-n-j G:i:s", $time): 2030-11-9 12:12:09
当月最后一天
date("t", $time): 30
date("Y-m-t", $time): 2030-11-30
date("Y-m-d H:i:s", strtotime(date("Y-m-01 H:i:s", $time) . "+1 month -1 day")): 2030-11-30 12:12:09


date: 2030-11-09 12:12:09
date: 2019-01-19 12:12:09
相差月份相差月份 : 142


date: 2030-11-09 12:12:09
date: 2030-11-19 22:32:09
相差天数 intval(abs($time1-$time2)/(60*60*24)): 10

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值