SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: strtotime

  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    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

    Off Topic:

    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
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    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, DateTime::createFromFormat()). That would look like:

    PHP Code:
    $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.

    PHP Code:
    $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(000$m$d$y));

    // go wild! 
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by kreut View Post
    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 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.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  6. #6
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OK!

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

    -Eric

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I processed dates submitted from jQuery datepicker like this:
    PHP Code:
                $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:
    Code:
    $('#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);  
        }
        });


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •