13

I have a form within which a date is input in UK format, and I need to convert it to

yyyy-mm-dd

e.g. a date entered is: 31/03/2013 which I want to convert to '2013-03-31' for database insert.

I'm using the following which bizarrely works only sometimes:

$dateInput = $mysqli->real_escape_string($_POST['date']);
$show_date = date('Y-m-d', strtotime($dateInput));

Is there a better way to do this?

StudioTime
  • 22,603
  • 38
  • 120
  • 207
  • Why are you escaping the date value.... does it actually serve any purpose at all? – Mark Baker Mar 28 '13 at 07:31
  • When is this not working properly? Also, escaping doesn't seem a good idea (or at least it seems useless if the date format is correct). Maybe you should consider using a regex to check the initial format. – ulentini Mar 28 '13 at 07:32
  • 1
    @Darren the reason this doesn't work sometimes is because PHP expects the date to be in US format. You have to re-arrange it before handing it over to `strtotime()`, if at all. http://stackoverflow.com/questions/4163641/php-using-strtotime-with-a-uk-date-format-dd-mm-yy – Tushar Mar 28 '13 at 07:32
  • @Uby I escape everything so some **** doesnt try injecting – StudioTime Mar 28 '13 at 07:34
  • 1
    @Darren - redundant in this case as you're converting the string to a datetime serialization and then back again - you have control... and if you think something could be injected, then escape doesn't do quite what you think – Mark Baker Mar 28 '13 at 07:38
  • you do not you use the checkdate function to validate the Date – Devesh Mar 28 '13 at 07:39
  • @MarkBaker Thanks for explaining that, I get it now. Good to know, appreciated. – StudioTime Mar 28 '13 at 07:46

7 Answers7

20

You might want to use DateTime::createFromFormat:

$show_date = DateTime::createFromFormat('d/m/Y', $dateInput)->format('Y-m-d');
sroes
  • 14,663
  • 1
  • 53
  • 72
11

try it.

$dateInput = explode('/','31/03/2013');
$ukDate = $dateInput[2].'-'.$dateInput[1].'-'.$dateInput[0];
Dieepak
  • 546
  • 2
  • 7
3

following which bizarrely works only sometimes:

$show_date = date('Y-m-d', strtotime($dateInput));

this is no other shorter way to do it.... and i am using this all my life.. havn't notice any bizzare things going on with it till now.. check if there is some other things that is messing up

otherway to do it is

$timestamp = strtotime(str_replace('/', '.', $dateInput));
$mysql_date = date('Y-m-d', $timestamp); 
bipen
  • 36,319
  • 9
  • 49
  • 62
  • 1
    With slashes, strtotime() expects a US-format date, not a UK-format date, which is why it would screw up for some values. – Tushar Mar 28 '13 at 07:33
  • 1
    I always wonder if 02/03/04 is 2nd March of 2004, 4th March of 2002, 3rd February of 2004 or... I think PHP always takes the last possibility, so this is not what is given in the input. – Voitcus Mar 28 '13 at 07:34
  • @Tushar Thanks, that makes sense when I look at the dates it was screwing up. – StudioTime Mar 28 '13 at 07:45
2

Try this :

$dte  = '28/03/2013';
$dt   = new DateTime();
$date = $dt->createFromFormat('d/m/Y', $dte);
echo $date->format('Y-m-d');

Output: 2013-03-28

Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
1

PHP 5.3 and up

Use DateTime::createFromFormat. It allows you to specify an exact mask - using the date() syntax - to parse incoming string dates with.

PHP 5.2 and lower

You will have to parse the elements (year, month, day, hour, minute, second) manually using substr() and hand the results to mktime() that will build you a timestamp.

Arvind
  • 938
  • 9
  • 23
1
$date_array = explode("/",$your_date); // split the array
$var_day = $date_array[0]; //day seqment
$var_month = $date_array[1]; //month segment
$var_year = $date_array[2]; //year segment
echo $new_date_format = "$var_year-$var_day-$var_month"; // join them  together

This will work

Dew Drop
  • 71
  • 1
  • 11
0

Couple of alternate methods. Both output 2013-03-31:

Method 1 - "Look ma, no functions"

<?php
    $in = '31/03/2013';
    echo $in[6].$in[7].$in[8].$in[9].'-'.$in[3].$in[4].'-'.$in[0].$in[1];
    ?>    

Method 2 - regex

<?php
    echo ($output = preg_replace('!^([0-9]{2})/([0-9]{2})/([0-9]{4})$!',"$3-$2-$1",$input));
?>
AbsoluteƵERØ
  • 7,816
  • 2
  • 24
  • 35