Strtotime

Hello!

I’m using the jquery calendar picker to get my users to enter dates in the form “dd/mm/yyyy”. I’d like to use php to convert this into a timestamp that I’ll upload to my database. I’m not quite sure what syntax of strtotime that I use so that php knows that my date is in the form above as opposed to, say, the European version mm/dd/yyyy. Any help would be appreciated.

Thanks so much,

Eric

The simplest approach would probably be to format the date within the datepicker. Take a look at the dateFormat option.

jQuery UI - Datepicker Demos & Documentation

[ot]dd/mm/yyyy is the European format, while mm/dd/yyyy is the American format (us Americans are weird in that we don’t follow the usual endian order of values with dates).

Date format by country - Wikipedia, the free encyclopedia[/ot]

Thanks for the reply! I fear that I’m a bit more versed in PHP at this stage of the game and I’m using PHP to write the date to my database; so it would be much easier if there’s a PHP based solution to convert dd/mm/yyyy from my textfield into a timestamp.

Other thoughts would be appreciated…

-Eric

You can’t use strtotime() directly with a dd/mm/yyyy formatted date as it expects that pattern of numbers to (incorrectly for your needs) be mm/dd/yyyy (see date formats). There are a couple of options, including a) using a method specifically designed for getting a date from a specified date format, or b) manipulating the date you have into one that strtotime() will like.

The first would be to use the date_create_from_format() function (or it’s identical twin, [url=http://php.net/datetime.createfromformat]DateTime::createFromFormat()). That would look like:


$input = '21/03/2011';
$date  = date_create_from_format('d/m/Y', $input);
echo date_format($date, 'Y-m-d');

// or
$date = DateTime::createFromFormat('d/m/Y', $input);
echo $date->format('Y-m-d');

However the above is only available with PHP 5.3, so if you aren’t using something that recent (why not?!), an alternative is to use some string manipulation to coerce the date into the format that suits you.


$input = '21/03/2011';

echo implode('-', array_reverse(explode('/', $input))); // 2011-03-21

echo vsprintf('%3$s-%2$s-%1$s', sscanf($input, '%2s/%2s/%4s')); // 2011-03-21

list($d, $m, $y) = explode('/', $input);
echo date('Y-m-d', mktime(0, 0, 0, $m, $d, $y));

// go wild!

You would just be over-complicating things unnecessarily. If the datepicker sends the format you’re looking for, no conversion would be necessary. Complicated = error-prone.

Go ahead and post the code for the datepicker. All that needs to be added is the dateFormat option.

OK!

Thank you both for alternate solutions to my situation. It looks like I have 2 clear paths that I can take.

-Eric

I processed dates submitted from jQuery datepicker like this:


            $date_array = explode('/', $_POST['weddingdate']);
        
            $date_array = array_map('intval', $date_array);
        
            if (sizeof($date_array) != 3)
            {
                $invalid[] = 'weddingdate';
            }
            else
            {
                list($day, $month, $year) = $date_array;
                
                if ( ! checkdate($month, $day, $year))
                {
                    $errors['weddingdate'] = 'The wedding date is not a valid date.';
                }
                else
                {
                    $wedding_date = "{$year}-{$month}-{$day}";   
                }
            }

Here is my js for the datepicker:


$('#weddingdate').datepicker(
{
    dateFormat: 'dd/mm/yy', 
    minDate: new Date(),
    maxDate: '+10y',
    changeMonth: true, 
    changeYear: true,
    buttonText: 'Select Wedding Date',
    firstDay: 1,
    showOn: 'both',
    showButtonPanel: true,
    closeText: "Close",
    constrainInput: true,
    onChangeMonthYear:function(y, m, i)
    {                                
        var d = i.selectedDay + "";
       
        if (d.length < 2) d = "0" + d;
       
        var m = m + "";
       
        if (m.length < 2) m = "0" + m;
        
        $('#weddingdate').val(d+"/"+m+"/"+y);  
    }
    });