Part of the EllisLab Network
   
 
[OPTIMIZATION] Date Helper > human_to_unix()
Posted: 06 May 2007 12:44 PM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  248
Joined  02-10-2007

system/helpers/date_helper.php - line 407


BEFORE: 10000 iterations take about 1.0424 seconds

function human_to_unix($datestr = '')
{
    
if ($datestr == '')
    
{
        
return FALSE;
    
}
    
    $datestr
= trim($datestr);
    
$datestr = preg_replace("/40+/", "40", $datestr);

    if ( !
ereg("^[0-9]{2,4}\-[0-9]{1,2}\-[0-9]{1,2}40[0-9]{1,2}:[0-9]{1,2}.*$", $datestr))
    
{
        
return FALSE;
    
}

    $split
= preg_split("/40/", $datestr);

    
$ex = explode("-", $split['0']);
    
    
$year  = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
    
$month = (strlen($ex['1']) == 1) ? '0'.$ex['1']  : $ex['1'];
    
$day   = (strlen($ex['2']) == 1) ? '0'.$ex['2']  : $ex['2'];

    
$ex = explode(":", $split['1']);
    
    
$hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
    
$min  = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];

    if (isset(
$ex['2']) AND ereg("[0-9]{1,2}", $ex['2']))
    
{
        $sec  
= (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
    
}
    
else
    
{
        
// Unless specified, seconds get set to zero.
        
$sec = '00';
    
}
    
    
if (isset($split['2']))
    
{
        $ampm
= strtolower($split['2']);
        
        if (
substr($ampm, 0, 1) == 'p' AND $hour < 12)
            
$hour = $hour + 12;
            
        if (
substr($ampm, 0, 1) == 'a' AND $hour == 12)
            
$hour =  '00';
            
        if (
strlen($hour) == 1)
            
$hour = '0'.$hour;
    
}
            
    
return mktime($hour, $min, $sec, $month, $day, $year);
}


AFTER: 10000 iterations take about 0.1571 seconds

function human_to_unix($datestr = '') {
    
return strtotime($datestr);
}
 Signature 

Kohana rocks!

Profile
 
 
Posted: 06 May 2007 04:09 PM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  30
Joined  03-26-2007

Agreed.  Or just do away with human_to_unix() entirely.  It’s obviously a reinvention of the wheel as the original author was not aware of strtotime()

Profile
 
 
Posted: 08 May 2007 10:53 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  17
Joined  09-20-2006

This change also improves flexibility of rather than imposing a new limit because strtotime will take many more formats than the manually coded human_to_unix.  So it is a feature in addition to an optimization.  But I am still in favor of eventually deprecating it.  I would think the best thing to do here would be in separate releases do the following: 1)to put this change in place 2)deprecate the human_to_unix in the manual 3)deprecate it further with a warning in the logs each time it is used 4)pull it entirely.

Profile
 
 
Posted: 08 May 2007 12:44 PM   [ Ignore ]   [ # 3 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  248
Joined  02-10-2007

I fully agree on the deprecation of this function. As ZareMedia said, it clearly is a reinvention of the wheel. People should just use the native PHP function. No need for a renamed wrapper function.

 Signature 

Kohana rocks!

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 77517 Total Logged-in Users: 29
Total Topics: 101528 Total Anonymous Users: 2
Total Replies: 544284 Total Guests: 263
Total Posts: 645812    
Members ( View Memberlist )