Jump to content

Date Formatting Problem


Go to solution Solved by requinix,

Recommended Posts

  • Solution

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.

Edited by requinix
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.