0

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.

7
  • 4
    What's $_POST['post_date'] currently? Commented Oct 21, 2011 at 20:58
  • $_POST['post_date'] is actually an array (updating multiple records), but an example value would be 'December 31, 2012' Commented Oct 21, 2011 at 21:08
  • 1
    Agree with genesis. I just tested it with PHP5.3 and it worked fine. One thing to keep in mind, though, is that strtotime will often read #-#-# as Year-Month-Date. If you want different behavior you might need to parse it more thoroughly first. Commented Oct 21, 2011 at 21:09
  • 2
    You can't use strtotime() on an array. Are you sure you're passing a string to it, not an array? Commented Oct 21, 2011 at 21:10
  • 1
    Can you do echo $_POST['post_date'] just above that line and tell us what it outputs? It works fine for me given your example inputs. Commented Oct 21, 2011 at 21:28

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

5 Comments

Commas can confuse strtotime(), but I don't think it will always break it.
I didn't downvote, but I just tested strtotime() with January 2, 2011 and it worked fine. Maybe different versions, ini, systems, etc.
@Dano See codepad.org/F9c2DHMh and codepad.org/R4kaWuWo In both cases they both break strtotime. I think this answer fits perfectly with this question and doesn't warrant a downvote. Yes it could boil down to different versions. I suggest he just removes the comma and run it.
@Mob the comma only breaks when you add the time to the date with comma. The date with comma but without hour and seconds gives expected results. codepad.org/qgJIJSaw
@ghbarratt Ok. Thanks. I did not know.
2

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)));
}

2 Comments

++ For input concerns. That's the major thing here.
Unfortunately, strtotime will not assume d-m-y, but will actually assume y-m-d. codepad.org/vbZ0tObH
1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.