date format mysql and php
07-19-06, 08:22 AM
Coding Addict
Join Date: Jul 2006
Location: Northumberland
Posts: 375
Thanks: 0
Thanked 0 Times in 0 Posts
date format mysql and php
I am trying to display the date in the "l dS F Y" format on my web page for each entry in the database. I have found plenty of articles on how to change the format for todays date but not for the actual date entered in a field.
my database field is named date and is of type "date". the entered date is 0000-00-00 format. This is the code I am using to try and convert it for display on the web page:
PHP Code:
$connection = mysql_connect ( $host , $usr , $pwd );
if ( $date ) { $date_sql = "and date LIKE '% $date %' " ; }
if ( $venue ) { $venue_sql = "and venue LIKE '% $venue %' " ; }
$query = mysql_db_query ( $db , "SELECT * FROM table_gigs where id > 0 $date_sql $venue_sql order by date desc" , $connection );
check_mysql ( $query );
$filename = "list_header.html" ;
$handle = fopen ( $filename , "r" );
$contents = fread ( $handle , filesize ( $filename ));
$contents = str_replace ( XrowsX , mysql_num_rows ( $query ), $contents );
print $contents ;
fclose ( $handle );
while ( $row = mysql_fetch_array ( $query )) {
$date = $row [ "date" ];
$date = date ( "l dS F Y" , $date ); /* the bit in question */
$venue = $row [ "venue" ];
$id = $row [ "id" ];
$filename = "list.html" ;
$handle = fopen ( $filename , "r" );
$contents = fread ( $handle , filesize ( $filename ));
$contents = str_replace ( Xgigs_titleX , $gigs_title , $contents );
$contents = str_replace ( Xheading_gigsX , $heading_gigs , $contents );
$contents = str_replace ( XdateX , $date , $contents );
$contents = str_replace ( XvenueX , $venue , $contents );
$contents = str_replace ( Xbutton1X , $button1 , $contents );
$contents = str_replace ( Xbutton2X , $button2 , $contents );
$contents = str_replace ( Xbutton3X , $button3 , $contents );
$contents = str_replace ( Xbutton4X , $button4 , $contents );
$contents = str_replace ( Xbutton5X , $button5 , $contents );
$contents = str_replace ( XidX , $id , $contents );
print $contents ;
fclose ( $handle );
}
any ideas?
__________________
Aye!
07-19-06, 08:39 AM
Community Leader
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
07-19-06, 09:08 AM
Coding Addict
Join Date: Jul 2006
Location: Northumberland
Posts: 375
Thanks: 0
Thanked 0 Times in 0 Posts
The simple things! Genius once again. Thanks nico
__________________
Aye!
03-27-08, 11:20 AM
New Member
Join Date: Oct 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
And what if the date field is empty. I mean 0000-00-00.
With the above solution it shows 30/11/1999 for me.
Any help please.
03-27-08, 11:35 AM
Code Master
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Well, I tried the following
and as expected, I got the Unix epoch date
Even with the string set to nothing, I got the same result. How you came to get 30/11/1999 I'll never know. Very random
03-27-08, 12:04 PM
New Member
Join Date: Oct 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Can we get the result as 00/00/0000 instead?
03-27-08, 12:23 PM
Code Master
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
amkma
Can we get the result as 00/00/0000 instead?
Not using
strtotime no, as it returns a unix time, so 0 will be 1/1/1970, as that is the start of the
Unix epoch
03-27-08, 12:52 PM
New Member
Join Date: Oct 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
any alternative?
Or instead of 00/00/0000 can we get a blank result?
Regards
03-27-08, 02:42 PM
Code Master
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
amkma
any alternative?
Or instead of 00/00/0000 can we get a blank result?
Regards
not if you use the strtotime function no. you will have to test for it seperately beforehand
03-28-08, 05:06 AM
Community Liaison
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
there's a very simple workaround: you just use an if!
__________________
"Good judgement comes from experience, and experience comes from bad judgement." -
Fred Brooks
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off