ianhaney50 Posted July 14, 2015 Share Posted July 14, 2015 I am getting the data storing as yyyy-mm-dd in the database now which is good but can't work out how to echo the data as dd-mm-yyyy format on the candidates profile php page I currently have the following echo "Date of Birth: " . $row ['dob']; I have tried a few ways but can't remember what they were now as few diff ways Quote Link to comment https://forums.phpfreaks.com/topic/297291-echo-php-date/ Share on other sites More sharing options...
fastsol Posted July 14, 2015 Share Posted July 14, 2015 echo "Date of Birth: " . date("d-m-Y", strototime($row ['dob'])); Quote Link to comment https://forums.phpfreaks.com/topic/297291-echo-php-date/#findComment-1516332 Share on other sites More sharing options...
fastsol Posted July 14, 2015 Share Posted July 14, 2015 I also hope that you are storing the date in the db in a DATE appropriate data type for the field. Something like DATE or DATETIME or TIMESTAMP Quote Link to comment https://forums.phpfreaks.com/topic/297291-echo-php-date/#findComment-1516333 Share on other sites More sharing options...
ianhaney50 Posted July 14, 2015 Author Share Posted July 14, 2015 (edited) I just sorted it using the following in my SELECT query, also sorry yeah am storing date as date type in the database , DATE_FORMAT(dob, '%e %M %Y') as dob Edited July 14, 2015 by ianhaney50 Quote Link to comment https://forums.phpfreaks.com/topic/297291-echo-php-date/#findComment-1516334 Share on other sites More sharing options...
scootstah Posted July 14, 2015 Share Posted July 14, 2015 Another way is this: SELECT UNIX_TIMESTAMP(dob) as dob ... echo date('d-m-Y', $row['dob']);This will cause MySQL to return a UNIX timestamp of your DATE field, and then PHP can format it with date(). It's useful if you have standardized date formatting logic and don't want to add the format to every single query. Quote Link to comment https://forums.phpfreaks.com/topic/297291-echo-php-date/#findComment-1516337 Share on other sites More sharing options...
mac_gyver Posted July 14, 2015 Share Posted July 14, 2015 unfortunately, for things like dates of birth, the abomination leftover from the 1970's, known as a unix timestamp, is not appropriate. under mysql, any date earlier than 1970-01-01 00:00:00 results in a zero (all current mysql versions) and depending on your php version and operating system, php date()/strototime() are either limited to 1970-01-01 00:00:00 or 1901-12-13 20:45:54. also, using a unix timestamp requires a conversion that is time zone dependent and is lossy because the mapping is not one-to-one in both directions due to DST. using the mysql DATE_FORMAT() however only involves changing the format of the value, not converting it to/from a different number system and avoids all these problems. the php date/time class doesn't have these problems either. Quote Link to comment https://forums.phpfreaks.com/topic/297291-echo-php-date/#findComment-1516344 Share on other sites More sharing options...
scootstah Posted July 14, 2015 Share Posted July 14, 2015 You raise good points. It all depends what you're doing, I suppose. I've been mainly working on backend services for quite a while now, so I like to just return a UNIX timestamp and let the client figure out how they want it formatted. Quote Link to comment https://forums.phpfreaks.com/topic/297291-echo-php-date/#findComment-1516346 Share on other sites More sharing options...
Barand Posted July 14, 2015 Share Posted July 14, 2015 I just sorted it using the following in my SELECT query, also sorry yeah am storing date as date type in the database , DATE_FORMAT(dob, '%e %M %Y') as dob Sorting on that formatted value won't work, for the same reason we don't store like that in the first place. Use the formatted version for your output but you need ORDER BY dob Quote Link to comment https://forums.phpfreaks.com/topic/297291-echo-php-date/#findComment-1516348 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.