from varchar date yyyy-mm-dd convert in php page to dd-m-yyyy

Hi everyone,

I have a problem. In the database I have a column with a date (varchar (100)
. It is entered in the database as yyyy-mm-dd. the name of the field is "pickupdate". I want to show them on a php page as: dd-mm-yyyy.
But I think I'm doing something wrong because he keeps displaying the date incorrectly.

this is what I had:
-------------------------------------------------- -------------------------------------------
$originalDate = $ _POST ["pickupdate"];
$newDate = date ("d-m-Y", strtotime ($originalDate));

echo 'We have received a pick up order for a '. $ _ POST ["yesno"].' to retrieve on '. $newDate.' <br /> <br />';
-------------------------------------------------- -------------------------------------------------- ------
I hope someone can help
thank you
Ron BaasDirecteurAsked:
Who is Participating?
Chris StanyonCommented:
OK Ron,

Fundamentally, there's nothing wrong with your code. If your date is indeed in the yyyy-mm-dd format, then the rest of your code will work fine:

$originalDate = "2018-04-23";
$newDate = date ("d-m-Y", strtotime ($originalDate));
echo $newDate; // 23-04-2018

Open in new window

However, there's still a little confusion. You're still referring to a database column called pickupdate, but then your code uses a POST variable called pickupdate. The Database and POST are very different things!

There's also a space between $ and _POST (guessing that's a typo here)

As a quick sanity check, do a var_dump of the POST array at the beginning of your code:. Also, whilst debuffing, it makes sense to turn on error reporting:

ini_set('display_errors', 1);


$originalDate = $_POST["pickupdate"];
$newDate = date("d-m-Y", strtotime($originalDate));
echo $newDate;

Open in new window

This way you will see exactly what your POST array holds and whether the values are what you expect them to be. You will also get an early warning of any errors in your code.
Chris StanyonCommented:
You say you have a database column called assignment date, but your code doesn't mention that at all. The only mention of a date is a POST variable called "pickup date" (won't work with a space in the name!!)

In PHP, if you have a date string in the yyyy-mm-dd format, then you can very easily use the DateTime object:

$originalDate = "2018-04-23";
$myDate = new DateTime($originalDate);

And then when you want to echo it out, you pass a format string into the format() method:

echo $myDate->format("d-m-Y");

To give you specific answers, you may want to clarify the question a little more
ste5anSenior DeveloperCommented:
Just a comment:
I have a problem. In the database I have a column with a date (varchar (100)).
Indeed. This is a severe design issue. You should correct that.
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

Ron BaasDirecteurAuthor Commented:
Hi Chris

Sorry, I made a few language mistakes. I have adjusted the question.

I hope you can help me
Ron BaasDirecteurAuthor Commented:
Thanks Chris,

It works

regards ron
Ron BaasDirecteurAuthor Commented:
thanks Chris
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.