I have tried using date("m/d/Y", strtotime("04-05-2012"))
but I will get "05/04/2012"
or on some other dates for example "03-30-2012"
I will get "12/31/1969"
(which makes sense because it it mixing up the month and day and there is no 30th month. So how should I do this? I also want to then convert the value into a UNIX time so that I can search it against MySQL db.
Asked
Active
Viewed 247 times
1

Ray
- 2,713
- 3
- 29
- 61
-
7`str_replace('-', '/', '04-05-2012')`? – gen_Eric Mar 27 '12 at 18:35
-
possible duplicate of [Convert date format for db](http://stackoverflow.com/questions/7697609/convert-date-format-for-db) – jprofitt Mar 27 '12 at 18:38
-
2Different countries have wildly different ideas about whether you should start with month or day. Is 04-05-2012 April 5th, or May 4th? Even Canada and USA have differing views on that one. Probably a better idea to switch to YYYY-mm-dd as your format. It also sorts better. – ghoti Mar 27 '12 at 18:40
-
`03-30` is march 30th, not the 3rd of month 30. – Marc B Mar 27 '12 at 18:40
-
I think I will do what @ghoti mentioned and use YYYY-mm-dd, I think it will work for what I need it to do. – Ray Mar 27 '12 at 18:50
4 Answers
8
You can use the DateTime object and createFromFormat static method to do it :
$date = DateTime::createFromFormat('m-d-Y',"03-30-2012");
$date->format('m/d/Y');
-
1That looks really helpful but we're using PHP 5.1.6 and that function wasn't implemented until 5.3.0. – Ray Mar 27 '12 at 18:44
-
perhaps a migration would be cool. We're now on php 5.4, and php 5.3.10 is out with more than 1,000 bug fixes and perf improvement. And some feature additions like DateTime, Intl... – artragis Mar 28 '12 at 15:14
6
If you know for certain that the format you start with is DD-MM-YYY when why not use a simple replace?
e.g. $newDate = str_replace('-', '/', '04-05-2012');

Ian Devlin
- 18,534
- 6
- 55
- 73
1
One way to do it would be using explode()
and mktime()
:
$inDate = '03-30-2012';
list($m, $d, $y) = explode('-', $inDate);
$outDate = date('m/d/Y', mktime(0, 0, 0, $m, $d, $y));
This assumes the format is somehow dynamic, though. Otherwise, str_replace()
is your best option, as others pointed out.

FtDRbwLXw6
- 27,774
- 13
- 70
- 107
0
This isn't so much a date format question as a string manipulation question.
But it's still good to know that strtotime()
exists.
[ghoti@pc ~]$ cat transdate.php
#!/usr/local/bin/php
<?php
$olddate = "04-05-2012"; // assuming mm-dd-YYYY
// Get the date parts into an array
$parts = explode("-", $olddate);
// Switch to YYYY-mm-dd, which will be interpreted consistently
$neworder = sprintf("%s-%s-%s", $parts[2], $parts[0], $parts[1]);
printf("New order: %s\n", $neworder);
// Set your timezone, or PHP will whine and complain
date_default_timezone_set('America/Toronto');
// Convert your reordered date to an epoch second (unix timestamp)
$epoch = strtotime($neworder);
// At a terminal, `man strftime` (or read the PHP function's docs) for details.
print "Alternate formats:\n";
printf("\t%s\n", strftime("%D", $epoch));
printf("\t%s\n", strftime("%F", $epoch));
printf("\t%s\n", strftime("%A %B %e, %Y (week %U)", $epoch));
[ghoti@pc ~]$ ./transdate.php
New order: 2012-04-05
Alternate formats:
04/05/12
2012-04-05
Thursday April 5, 2012 (week 14)
[ghoti@pc ~]$
This will work in PHP 5.1.6. Heck, it should work in PHP 4, except for date_default_timezone_set()
.

ghoti
- 45,319
- 8
- 65
- 104