Reputation: 169
I am trying to display the date that I send by $_GET
, my url format is for example http://127.0.0.1/index.php?date=01/08/2018
.
The result is 08-01-2018
it change the order of the day and the month why ?
$orginal_date= $_GET['date'];
$date = date("d-m-Y", strtotime($original_date));
echo $date;
Upvotes: 0
Views: 818
Reputation: 11
Try This
list($month, $day, $year) = explode("/", $_GET["date"]);
echo date('d-m-Y',mktime(0, 0, 0, $month,$day, $year));
Upvotes: 0
Reputation: 26
This looks like you may be assuming how php will use strtotime and convert to a timestamp. By default it thinks that 01/08/2018 is Jan 8 2018. There is a method date_create_from_format that you can tell it the format you are expecting and then you can format that to however you want it to look like.
$orig = $_GET['date'];
$date = date_create_from_format('d/m/Y', $orig);
echo date_format($date, 'd-m-Y');
//result is 01-08-2018
Upvotes: 0
Reputation: 9135
list($month, $day, $year) = explode("/", $_GET["date"]);
$date = sprintf("%d-%02d-%02d", $year, $month, $day);
Upvotes: 1