AVProgrammer
AVProgrammer

Reputation: 1350

PHP date - String to Timestamp

I am trying to parse date strings without having to manipulate strings and expect a given format.

I want my user to be able to enter January 2, 2011 or 1-2-11, and still end up with 2011-01-02 12:00:00 to save in a database.

Currently, I have this in place:

$post_date = date("Y-m-d H:i:s", strtotime(stripslashes($_POST['post_date'])));

But it seems strtotime is returning 0, because the datetime ends up as 1969-12-31 17:00:00

What do I need to change?


UPDATE

From php.net:

(PHP 4, PHP 5)

strtotime — Parse about any English textual datetime description into a Unix timestamp.

..I guess not!


I am not trying to strtotime( an array:

if(isset($_POST['post_date'])){
                foreach($_POST['post_date'] as $id => $post_date){
                    print $post_date.'<br />';
                    if(strlen(trim($post_date)) && $post_date !== 'Date posted'){
                        $update_data[$id]['post_date'] = date("Y-m-d H:i:s", strtotime(stripslashes($post_date)));
                    }
                }
            }

Got it working with:

if(isset($_POST['post_date'])){
                foreach($_POST['post_date'] as $id => $post_date){
                    print $post_date.'<br />';
                    if(strlen(trim($post_date)) && $post_date !== 'Date posted'){
                        $post_date = str_replace(',', '', $post_date);
                        $post_date = str_replace('-', '/', $post_date);
                        $update_data[$id]['post_date'] = date("Y-m-d H:i:s", strtotime(stripslashes($post_date)));
                    }
                }
            }

Thanks to multiple contributors. I remove commas and replace hyphens with forward slashes.

Upvotes: 0

Views: 14431

Answers (3)

Dan
Dan

Reputation: 594

You really need to be concerned with your input, but here's an idea.

foreach($_POST['input'] as $userDate) {
    // strtotime() will assume m/d/y or y-m-d depending on if '-' of '/' is used.
    $userDate = str_replace('-', '/', $userDate);
    $post_date = date("Y-m-d H:i:s", strtotime(stripslashes($userDate)));
}

Upvotes: 2

ghbarratt
ghbarratt

Reputation: 11711

In PHP 5.3.3+ (and perhaps older versions)

date('Y-m-d', strtotime('January 2, 2011'))

(notice the comma IS there) will give you 2011-01-02

However, when you add hour and minute to the end of that date phrase, strtotime DOES return 0.

date('Y-m-d', strtotime('January 2, 2011 14:30'))

Unfortunately gives you 1970-01-01 00:00:00 Notice: http://codepad.org/qgJIJSaw

Consider removing the comma:

$date = str_replace(',', '', $date);

Also, strtotime will convert '1-2-11' to 2001-02-11 (February 11th 2001), so you will probably need to rearrange the numbers if they fit the pattern, using something like:

 $date = preg_replace('#^([\d]{1,2})-([\d]{1,2})-([\d]{2,4})$#', '$3-$1-$2', $date);

Upvotes: 1

Mob
Mob

Reputation: 11106

From the comments the OP said :

$_POST['post_date'] is actually an array (updating multiple records), but an example value would be 'December 31, 2012'

You cannot pass a comma in the strtotime arguments alongside time, doing so would always return a 1970-01-01 00:00:00. You have to remove the user generated comma.

$post_date = date("Y-m-d H:i:s", strtotime(stripslashes("1 January 1927 17:59")));
echo $post_date; //1927-01-01 17:59:00

Upvotes: 4

Related Questions