htmlpower
htmlpower

Reputation: 169

Php change the order of the date

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

Answers (4)

Vinod Bansal
Vinod Bansal

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

Ben Harrington
Ben Harrington

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

Markus Zeller
Markus Zeller

Reputation: 9135

list($month, $day, $year) = explode("/", $_GET["date"]);
$date = sprintf("%d-%02d-%02d", $year, $month, $day);

Upvotes: 1

AymDev
AymDev

Reputation: 7594

It automatically parses the date with format m/d/Y (american format). You could use the DateTime class to specify your format:

$date = DateTime::createFromFormat('d/m/Y', $_GET['date']);
echo $date->format('d-m-Y');

Documentation:

Upvotes: 3

Related Questions