Jump to content

Date Formatting Problem


refiking

Recommended Posts

And the problem is...?

 

strtotime() doesn't accept m-d-Y format. If you know that's the format of the input then you need to get the pieces yourself.

if (preg_match('/^(\d\d?)-(\d\d?)-(\d\d\d?\d?)$/', $_POST["date_added"], $matches) && checkdate((int)$matches[1], (int)$matches[2], (int)$matches[3]) {
    // assume it's m-d-Y format
    $date = mktime(0, 0, 0, (int)$matches[1], (int)$matches[2], (int)$matches[3]);
} else {
    $date = strtotime($_POST["date_added"]);
}

Or if you don't mind using m/d/Y format (ie, with slashes instead of hyphens) that will work.

 

Even better: unless you need lots of flexibility with dates, give the user a datepicker-type widget and you can always get a standard format (like Y-m-d) back from your form.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.