我们在利用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/>";
时间: 2024-10-12 03:52:47