note 109049 modified in function.strtotime by danbrown

From: Date: Fri, 15 Jun 2012 21:19:52 +0000
Subject: note 109049 modified in function.strtotime by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-188639@lists.php.net to get a copy of this message
This example shows common methods to get a timestamp from $year,$month,$day variables.&#13;&#10;strtotime() with implode() seems to be the fastest way (performance wise..)&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;&#13;&#10;&#9;function logTime($start,$end,$functionName){&#13;&#10;&#9;&#9;echo &#34;$functionName() = &#34;.(float)($end-$start)*(60*60).&#34;&#60;br&#62;&#34;;&#13;&#10;&#9;}&#13;&#10;&#13;&#10;&#9;$day = &#39;01&#39;;&#9;$month = &#39;01&#39;;&#9;$year = &#39;2000&#39;;&#13;&#10;&#13;&#10;&#9;$time = array();&#13;&#10;&#9;$timestamps = array();&#13;&#10;&#9;$time[] = array(microtime(true),&#39;start&#39;);&#13;&#10;&#13;&#10;&#9;$timestamps[] = mktime(0,0,0,$month,$day,$year);&#13;&#10;&#9;$time[] = array(microtime(true),&#39;mktime&#39;);&#13;&#10;&#13;&#10;&#9;$timestamps[] = strtotime($year.&#39;-&#39;.$month.&#39;-&#39;.$day);&#13;&#10;&#9;$time[] = array(microtime(true),&#39;strtotime&#39;);&#13;&#10;&#13;&#10;&#9;$timestamps[] = strtotime(implode(&#39;-&#39;, array($year, $month, $day)));&#13;&#10;&#9;$time[] = array(microtime(true),&#39;strtotime_Array&#39;);&#13;&#10;&#13;&#10;&#9;$timestamps[] = date_create_from_format(&#39;!Y-m-d&#39;, $year.&#39;-&#39;.$month.&#39;-&#39;.$day)-&#62;getTimestamp();&#13;&#10;&#9;$time[] = array(microtime(true),&#39;date_create_from_format&#39;);&#13;&#10;&#13;&#10;&#9;$timestamps[] = date_create_from_format(&#39;!Y-m-d&#39;, implode(&#39;-&#39;, array($year, $month, $day)))-&#62;getTimestamp();&#13;&#10;&#9;$time[] = array(microtime(true),&#39;date_create_from_format_Array&#39;);&#13;&#10;&#13;&#10;&#9;$timestamps[] = DateTime::createFromFormat(&#39;!Y-m-d&#39;, $year.&#39;-&#39;.$month.&#39;-&#39;.$day)-&#62;getTimestamp();&#13;&#10;&#9;$time[] = array(microtime(true),&#39;DateTime&#39;);&#13;&#10;&#13;&#10;&#9;$timestamps[] = DateTime::createFromFormat(&#39;!Y-m-d&#39;, implode(&#39;-&#39;, array($year, $month, $day)))-&#62;getTimestamp();&#13;&#10;&#9;$time[] = array(microtime(true),&#39;DateTime_Array&#39;);&#13;&#10;&#13;&#10;&#9;foreach ($time as $key=&#62;$value) {&#13;&#10;&#9;&#9;if(end($time) == $value){}else{&#13;&#10;&#9;&#9;&#9;logTime($value[0],$time[$key+1][0],$time[$key+1][1]);&#13;&#10;&#9;&#9;}&#13;&#10;&#9;}&#13;&#10;&#13;&#10;&#9;// just to show that all timestamps are correct&#13;&#10;&#9;var_dump($timestamps);&#13;&#10;?&#62; --was-- This example shows common methods to get a timestamp from $year,$month,$day variables. strtotime() with implode() seems to be the fastest way (performance wise..) <?php function logTime($start,$end,$functionName){ echo "$functionName() = ".(float)($end-$start)*(60*60)."<br>"; } $day = '01'; $month = '01'; $year = '2000'; $time = array(); $timestamps = array(); $time[] = array(microtime(true),'start'); $timestamps[] = mktime(0,0,0,$month,$day,$year); $time[] = array(microtime(true),'mktime'); $timestamps[] = strtotime($year.'-'.$month.'-'.$day); $time[] = array(microtime(true),'strtotime'); $timestamps[] = strtotime(implode('-', array($year, $month, $day))); $time[] = array(microtime(true),'strtotime_Array'); $timestamps[] = date_create_from_format('!Y-m-d', $year.'-'.$month.'-'.$day)->getTimestamp(); $time[] = array(microtime(true),'date_create_from_format'); $timestamps[] = date_create_from_format('!Y-m-d', implode('-', array($year, $month, $day)))->getTimestamp(); $time[] = array(microtime(true),'date_create_from_format_Array'); $timestamps[] = DateTime::createFromFormat('!Y-m-d', $year.'-'.$month.'-'.$day)->getTimestamp(); $time[] = array(microtime(true),'DateTime'); $timestamps[] = DateTime::createFromFormat('!Y-m-d', implode('-', array($year, $month, $day)))->getTimestamp(); $time[] = array(microtime(true),'DateTime_Array'); foreach ($time as $key=>$value) { if(end($time) == $value){}else{ logTime($value[0],$time[$key+1][0],$time[$key+1][1]); } } // just to show that all timestamps are correct var_dump($timestamps); http://php.net/manual/en/function.strtotime.php

« previous php.notes (#188639) next »