1

In an SQL database I'm storing various dates, such as date of birth and date they joined my system, in the standard SQL format YYYY-mm-dd, however I wish to display these to my British users (all my users) in the format dd-mm-YYYY.

I've tried pretty much everything I found online about doing this, however cannot decipher how it's done correctly. The code I list below is what I am currently using, however it does not display the correct date stored in the database and instead uses a completely random date of 01-01-1970. Some assistance on resolving this issue would be greatly appreciated.

while($row = $results->fetch_assoc()){
                array_push($_SESSION["ActQueue"], array($row["username"], $row["surname"], $row["forename"], date('d-m-Y', $row["dob"]), $row["gender"], $row["joined"]));
}

$data = 0;
echo json_encode(['Username'=>$_SESSION["ActQueue"][0][0], 'Surname'=>$_SESSION["ActQueue"][0][1],'Forename'=>$_SESSION["ActQueue"][0][2],'DoB'=>$_SESSION["ActQueue"][0][3], 'Gender'=>$_SESSION["ActQueue"][0][4], 'Joined'=>$_SESSION["ActQueue"][0][5]]);

3 Answers 3

1

You need to convert your plain text date to time before passing to date() function

date('d-m-Y', strtotime($row["dob"]))

The date you receive 01-01-1970 its not a random date but its actually the first date from unix system

Sign up to request clarification or add additional context in comments.

9 Comments

This has the intended effect, but adds an extra "-", like this: Date of Birth: 30-11--0001. Any suggestions?
What do you mean? You can add whatever you need in date function like two minus symbols
It changes the date to dd-mm-YYYY format, but adds an extra "-" for some reason, like 30-11--0001.
How does it look row['dob'] if you print before pass to date function?
strtotime($row["dob"]) outputs -62169984000. $row["dob"] outputs 0000-00-00
|
1

You need to use :

date('d-m-Y', strtotime($row["dob"]))

strtotime

Comments

0

You can use this this code to format your date

(new \DateTime($row["dob"]))->format('d-m-Y');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.