centenial Posted December 12, 2006 Share Posted December 12, 2006 Hi,I have a database table with several hundred records. The DB table has these fields:- id- name- dateAll the dates are in YYYY-MM-DD format. However, when they're displayed on my website, I want the dates to display like this:Jan 1, 2006 orDec 12, 2006 (whatever the correct date is)Is there a function in PHP that I can use to convert YYYY-MM-DD to the more elegant format above?Thanks for your help, Link to comment https://forums.phpfreaks.com/topic/30398-php-date-format/ Share on other sites More sharing options...
craygo Posted December 12, 2006 Share Posted December 12, 2006 first off you should change your date field name to rdate or something other than date. Can cause problems down the line.second, what format is your date field. Is it actually a date field or a timestamp or just a string.Look up the date() function.[code]$row_date = date("M j, Y", strtotime("date field here"));[/code]That will work with actual date fields. If it is a timestamp you will have to use mktime to get the correct date.[code]<?php$timestamp = "20061212020250";$row_date = date("M j, Y", mktime(0, 0, 0, substr($timestamp, 4, 2), substr($timestamp, 6, 2), substr($timestamp, 0, 4)));?>[/code]It all depends on how you are storing your datesRay Link to comment https://forums.phpfreaks.com/topic/30398-php-date-format/#findComment-139883 Share on other sites More sharing options...
emehrkay Posted December 12, 2006 Share Posted December 12, 2006 *edit*i provided an anwser to an unasked question :D Link to comment https://forums.phpfreaks.com/topic/30398-php-date-format/#findComment-139887 Share on other sites More sharing options...
monkey_05_06 Posted December 12, 2006 Share Posted December 12, 2006 The second parameter to PHP's date function is optional and defaults to the value of PHP's time function which returns a timestamp for the current date/time. Link to comment https://forums.phpfreaks.com/topic/30398-php-date-format/#findComment-139891 Share on other sites More sharing options...
craygo Posted December 12, 2006 Share Posted December 12, 2006 Crap I blew right by this part[code]All the dates are in YYYY-MM-DD format. However, when they're displayed on my website, I want the dates to display like this:[/code]So use my first suggestionRay Link to comment https://forums.phpfreaks.com/topic/30398-php-date-format/#findComment-139896 Share on other sites More sharing options...
timmah1 Posted December 12, 2006 Share Posted December 12, 2006 I used the code that you provided[code]$row_date = date("M j, Y", strtotime("last_login"));[/code]I have a field called last_login, and it is type is datetimeWhen I use the code, it displays Dec. 31, 1969something i'm doing wrong? Link to comment https://forums.phpfreaks.com/topic/30398-php-date-format/#findComment-139910 Share on other sites More sharing options...
craygo Posted December 12, 2006 Share Posted December 12, 2006 The field should be in an array of some kind. Post your code for your query and will help.should be something like this[code]<?php$sql = "SELECT * FROM table_name":$res = mysql_query($sql) or die (mysql_error()); while($r=mysql_fetch_assoc($res)){ $row_date = date("M j, Y", strtotime($r['last_login'])); echo "$row_date<br>"; }?>[/code]Ray Link to comment https://forums.phpfreaks.com/topic/30398-php-date-format/#findComment-139918 Share on other sites More sharing options...
SharkBait Posted December 12, 2006 Share Posted December 12, 2006 Couldn't you use MySQL to format the time in the way you want?[code]SELECT DATE_FORMAT(myDateField, '%Y-%d-%m') as myDate FROM myTable;[/code]Though the formatting would have to be done properly but the idea is you can get the query to do it for you :) Link to comment https://forums.phpfreaks.com/topic/30398-php-date-format/#findComment-139919 Share on other sites More sharing options...
craygo Posted December 12, 2006 Share Posted December 12, 2006 Absolutely[code]<?php$sql = "SELECT DATE_FORMAT(last_login, '%b %c, %Y') AS lastlogin FROM myTable";$res = mysql_query($sql) or die (mysql_error()); while($r=mysql_fetch_assoc($res)){ echo $r['lastlogin']."<br>"; }?>[/code]That will give you the format you want.Ray Link to comment https://forums.phpfreaks.com/topic/30398-php-date-format/#findComment-139923 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.