Reputation: 3734
read and have re read the documentation and truly cannot explain why $date1 comes through bad
can someone help? Greatly appreciate it
$date1 = "04-16-2013";
$date2 = "2013-04-16";
printf("date1 = %s\n", $date1);
printf("date2 = %s\n", $date2);
$newdate1 = date('m-d-Y',$date1);
$newdate2 = date('Y-m-d',$date2);
printf("newdate1 = %s\n", $newdate1);
printf("newdate2 = %s\n", $newdate2);
$previous_date = date('m-d-Y', strtotime($date1 .' -1 day'));
$previous_date2 = date('m-d-Y', strtotime($date2 .' -1 day'));
printf("previous_date = %s\n", $previous_date);
printf("previous_date2 = %s\n", $previous_date2);
OUTPUT
date1 = 04-16-2013
date2 = 2013-04-16
newdate1 = 12-31-1969
newdate2 = 1969-12-31
previous_date = 12-31-1969
previous_date2 = 04-15-2013
Upvotes: 0
Views: 335
Reputation: 11117
strtotime function
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
In your case because you are using the separator dash (-) php consider that the date format is in d-m-y.
Or a workaround:
$date1 = "04-16-2013";
$date2 = "2013-04-16";
printf("date1 = %s\n", $date1);
printf("date2 = %s\n", $date2);
$newdate1 = date('m-d-Y',strtotime(str_replace("-","/",$date1)));
$newdate2 = date('Y-m-d', strtotime($date2));
printf("newdate1 = %s\n", $newdate1);
printf("newdate2 = %s\n", $newdate2);
$previous_date = date('m-d-Y', strtotime(str_replace("-","/",$date1) .' -1 day'));
$previous_date2 = date('m-d-Y', strtotime($date2 .' -1 day'));
printf("previous_date = %s\n", $previous_date);
printf("previous_date2 = %s\n", $previous_date2);
Upvotes: 1
Reputation: 12295
Try this in your case:
$date1 = "04-16-2013";
$date = date_create_from_format('m-j-Y', $date1);
echo date_format($date, 'd-m-Y');
First the input month-day-year....then outputs day-month-Year
Upvotes: 0