PHP有關時間戳記的操作

來源:互聯網
上載者:User

標籤:php   時間戳記   

我們在利用PHP開發程式的時候,總會遇到一些比較時間的困擾,現在我列舉一下幾種我們開發過程中常用的操作時間戳記的執行個體:


<?php//當天的開始(時間戳記)和結束(時間戳記)$date = date( "Y-m-d" ); //如‘2012-10-31‘list( $year, $month, $day ) = explode( ‘-‘, $date ); //分割後$year = ‘2012‘, $month = ‘10‘, $day = ‘31‘//將字串轉換成整型$year = intval( $year );$month = intval( $month );$day = intval( $day );$start_time = mktime( 0, 0, 0, $month, $day, $year ); //當天的起始時間(時間戳記),如2012-10-30 00:00:00的時間戳記$end_time   = mktime( 23, 59, 59, $month, $day, $year ); // 當天最後時間(時間戳記),如2012-10-31:23:59:59的時間戳記,也可以是$end_time = mktime( 0, 0, 0, $month, $day+1, $year ) - 1.//當月的第一天(如"2012-10-01")和最後一天(如"2012-10-30")$date = date("Y-m-d");$date = "2012-10-2";$month_first = date("Y-m-01", strtotime($date)); //當月的第一天$month_last  = date("Y-m-d", strtotime("$month_first +1 month -1 day"));//當月的最後一天,strtotime: 以$month_first的時間為基準,(+1 month)月份加1,(-1 day)天數-1(剛好是上個月的最後一天,strtotime返回的是時間戳記,用date函數轉格式)echo $month_first."<br/>".$month_last."<br/>";$date = date( "Y-m-d" );list( $year, $month, $day ) = explode( ‘-‘, $date ); //分割後$year = ‘2012‘, $month = ‘10‘, $day = ‘31‘//將字串轉換成整型$year = intval( $year );$month = intval( $month );$day = intval( $day );$month_day = date( "t", mktime( 0, 0, 0, $month, $day, $year ) );//該月一共有幾天echo $month_day."<br/>";$month_first = date( "Y-m-d", mktime(0, 0, 0, $month, 1, $year) );$month_last = date( "Y-m-d", mktime(0, 0, 0, $month, $month_day, $year) );//該月的總天數其實就是該的最後一天echo $month_first."<br/>".$month_last."<br/>";




本文出自 “開心編程” 部落格,請務必保留此出處http://happyliu.blog.51cto.com/501986/1547837

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.