note 109049 modified in function.strtotime by danbrown
| From: | danbrown@php.net | 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. 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); ?>
--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