php時間戳記

來源:互聯網
上載者:User

標籤:代碼   strong   sub   icon   second   --   exp   cti   ast   

  1. // 獲得零點的時間戳記

    $time = strtotime(date(‘Ymd‘));

  2.  

    // 獲得今天24點的時間戳記

    $time = strtotime(date(‘Ymd‘)) + 86400;

     

  3. // 上一天時間戳記

    $date = strtotime(‘-1 days‘);

     

    //擷取當天的年份

    $y = date("Y"); //擷取當天的月份$m = date("m"); //擷取當天的號數$d = date("d"); //將今天開始的年月日時分秒,轉換成unix時間戳記(開始樣本:2015-10-12 00:00:00)$todayTime= mktime(0,0,0,$m,$d,$y); //$todayTime即是當天零點的時間戳記

     

    php 擷取時間今天明天昨天時間戳記2011-06-30 9:46

    <?php

    echo "今天:".date("Y-m-d")."<br>";     
    echo "昨天:".date("Y-m-d",strtotime("-1 day")), "<br>";     
    echo "明天:".date("Y-m-d",strtotime("+1 day")). "<br>";  

    echo "一周后:".date("Y-m-d",strtotime("+1 week")). "<br>";     
    echo "一周零兩天四小時兩秒後:".date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")). "<br>";     
    echo "下個星期四:".date("Y-m-d",strtotime("next Thursday")). "<br>";     
    echo "上個周一:".date("Y-m-d",strtotime("last Monday"))."<br>";     
    echo "一個月前:".date("Y-m-d",strtotime("last month"))."<br>";     
    echo "一個月後:".date("Y-m-d",strtotime("+1 month"))."<br>";     
    echo "十年後:".date("Y-m-d",strtotime("+10 year"))."<br>";    

    strtotime()函數的作用是將日期時間描述解析為 Unix 時間戳記

    int strtotime  ( string time [, int now] )

    ?>

    本函數預期接受一個包含美國英語日期格式的字串並嘗試將其解析為 Unix 時間戳記(自 January 1 1970 00:00:00 GMT 起的秒數),其值相對於 now  參數給出的時間,如果沒有提供此參數則用系統目前時間。

    -------------------------------------------------------------------

     

    在PHP裡得到前天和昨天的日期的代碼
    前 天去面試的時候也是這樣,不過我當時記不起來了.就記得MYSQL裡面的date_sub(now(),‘interval 1 day‘);date(‘Y/m/d h:i:s‘,mktime(date(‘h‘), date(‘i‘), date(‘s‘), date(‘m‘) , date(‘d‘)+1, date(‘Y‘)));

    -------------------------------------------------------------------------------- 
    先得到今天的UNIXTIME 
    然後減去一天或兩天的秒數 
    把減後的UNIXTIME格式化成日期。 
    --------------------------------------------------------------------------------

    以下為引用的內容:
    <?php 
    date_default_timezone_set(‘Asia/Shanghai‘); 
    #昨天 
    echo date("Y/m/d h:i:s",time()-24*60*60); 
    echo "<br>"; 
    #前天 
    echo date("Y/m/d h:i:s",time()-2*24*60*60); 
    ?> 

    -------------------------------------------------------------------------------- 
    up 
    -------------------------------------------------------------------------------- 
    方法有很多種啊, 我也介紹一種吧: 
    date("Y/m/d H:i:s", strtotime("1 days ago")); 
    date("Y/m/d H:i:s", strtotime("2 days ago")); 
    -------------------------------------------------------------------------------- 
    date("Y/m/d H:i:s",mktime(0,0,0,date("m"),date("d")-1,date("Y"))); 
    -------------------------------------------------------------------------------- 
    以前算時間總是很煩人,呵呵,學了了下,下面是下個星期現在的時間。 
    date_default_timezone_set(‘Asia/Shanghai‘); 
    $tmp = time()+60*60*24*7; 
    print date("m/d/Y H:i:s", $tmp); 
    -------------------------------------------------------------------------------- 
    再加一個: 
    $time_yes=localtime(time()-24*60*60, true); 
    $time_b_yes=localtime(time()-2*24*60*60, true); 
    $yesterday=$time_yes[‘tm_mday‘]; 
    $the_day_before_yes=$time_b_yes[‘tm_mday‘]; 
    -------------------------------------------------------------------------------- 
    time()-86400 昨天的

    以下為引用的內容:

    <? 
    //昨天 
    print date(‘Y-m-d‘ , strtotime(‘-1 day‘)); 
    //上星期 
    print date(‘Y-m-d‘ , strtotime(‘-1 week‘)); 
    //上個月 
    print date(‘Y-m-d‘ , strtotime(‘-1 month‘)); 
    //去年 
    print date(‘Y-m-d‘ , strtotime(‘-1 year‘)); 
    ?>


    --------------------------------------------------------------------------------

    strtotime得到一個時間戳記, 然後你自己格式化. 
    strtotime(‘yesterday‘); 
    strtotime(‘-2 day‘);

     

php時間戳記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.