0

In my database I have 10-01-2021 (d/m/Y)
I want to display this on the website as (d F Y) "10 January 2021. However it shows as 1 October 2021

How do I tell the code that the day and the month are not in US order of month/day but actually as day/month?

I have tried using $projectCreated = DateTime::createFromFormat('d/M/Y', $projectCreated); but this doesn't echo anything.

$projectCreated = date_create($projectCreated);
$projectCreated = date_format($projectCreated,"d F Y");
Earl Grey
  • 59
  • 7
  • 1
    _"In my database I have (d-m-Y)"_ and _"I have tried 'd/M/Y'"_... see the error? – El_Vanja Jan 11 '21 at 14:02
  • 2
    _"In my database I have (d-m-Y)"_ - That means that you're not storing the dates correctly. When storing dates in mysql (assuming you're using that), you should set the column to `date` instead of `varchar`. Dates in mysql are in the format `Y-m-d`. If you store it as varchar, you need to convert it and reformat the dates in your queries to be able to use any of mysql's date-functions, or even to sort the results on the date. – M. Eriksson Jan 11 '21 at 14:10
  • The database is d/m/Y - My mistake I put - instead of / Yes I am aware it is stored as a string and not as a date. I want a string in the database. I am trying to format the string into date and manipulate as needed in the code. – Earl Grey Jan 11 '21 at 14:21
  • 1
    _"I want a string in the database"_ - That makes no sense. If you store data in a custom way like that, it just adds a lot of overhead for you (and the database) since you will need to convert it to a sensible format before you can do some real actions on them. There's not really any upside to it, just drawbacks. If you store the data correctly, you'll have way more tools built in for handling that data in a proper way – M. Eriksson Jan 11 '21 at 14:27
  • I agree with @MagnusEriksson. Either your store it as an SQL datetime object (his his own storage type and syntax) or you store it the sql as string datetime string (i hope i say that one right) which is that ackward second number since januari 1st 1970. – Dorvalla Jan 11 '21 at 14:45
  • @Dorvalla - I think you're referring to timestamp, which is number of seconds since 1970-01-01 and not datetime string. – M. Eriksson Jan 11 '21 at 15:01

1 Answers1

1

You should echo formatted output instead the static class function.

$date = DateTime::createFromFormat('d-m-Y', '10-01-2021');
echo $date->format('d F Y');
Ali Demirci
  • 5,302
  • 7
  • 39
  • 66