Welcome Guest, Not a member yet? Register   Sign In
Date helper - Human to unix with timezone support
#1

[eluser]Philo01[/eluser]
I'm currently working on a project where all time is stored in GMT.
I allow users to schedule an event using their local time.

I noticed that if you are using the human to unix function it will actually convert the given time and date to GMT. This is not very handy for example:

You schedule an event at (local UP1 + Daylight saving) 09/07/11 10:00:00 PM it will save this as 1310248800.
But if you to convert this back to the users local time using the gmt_to_local function you will notice that the time has shifted 2 hours 10/07/11 00:00:00 AM.

It's easy to solve luckily, create a new file inside application/helpers/MY_date_helper.php

Code:
/**
* Convert "human" (local) date to GMT
*
* Reverses the above process
*
* @access    public
* @param    string, timezone, daylight saving
* @return    integer
*/
    function human_to_unix($datestr = '', $timezone = '', $dst = FALSE)
    {
        if ($datestr == '')
        {
            return FALSE;
        }

        $datestr = trim($datestr);
        $datestr = preg_replace("/\040+/", ' ', $datestr);

        if ( ! preg_match('/^[0-9]{2,4}\-[0-9]{1,2}\-[0-9]{1,2}\s[0-9]{1,2}:[0-9]{1,2}(?::[0-9]{1,2})?(?:\s[AP]M)?$/i', $datestr))
        {
            return FALSE;
        }

        $split = explode(' ', $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']) && preg_match('/[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;
        }

        $unix = mktime($hour, $min, $sec, $month, $day, $year);
        
        if($timezone != '')
        {
            $unix -= timezones($timezone) * 3600;
            
            if ($dst == TRUE)
            {
                $unix -= 3600;
            }        
        }
        
        return $unix;
    }

This will store the time correctly using GMT Smile
Would be great is this could be integrated into CI by default.
#2

[eluser]adityamenon[/eluser]
Consider sending a commit on BitBucket. The CI main developers will decide whether or not to accept your code...




Theme © iAndrew 2016 - Forum software by © MyBB