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.
strtotime()on an array. Are you sure you're passing a string to it, not an array?echo $_POST['post_date']just above that line and tell us what it outputs? It works fine for me given your example inputs.