I've managed to get PHP to work well with SQL, but date and time-related parts of my site are proving difficult to understand.

I've created 3 records with these dates:
December 26th (2009) - 2:25pm
January 5th, 2010 - 3:00pm
January 7th, 2010 - 9:00pm

I'm trying to get it to show them in my script but am not sure how to set it correctly in PHPMyadmin or in PHP. It always shows the current date and time in my script, which is not what I want.

Basically, what I want to do is display the date and time as above, with the current year not being shown, only the next year's being shown, like this:

December 26th - 2:25pm
January 5th, 2010 - 3:00pm
January 7th, 2010 - 9:00pm

Also, I would like the record to be updated so that it changes it to this on the day itself (current date):

2:25pm

then removes the record after a period of time has elapsed (I don't want to do this manually, nor do I have access to cron jobs).

I'm not sure how to do this, and would welcome any advice.

I would suggest reading up on the following functions in the PHP documentation as they will come in handy when dealing with dates:
date
strtotime
strftime

For example, to find the current year, use the following code:

$now = strtotime("NOW"); // timestamp of current date/time
$currentYear = date("Y", $now); // format of "Y" gives four digit year ie 2009 not 09
if($yearOfDateFromDatabase == $currentYear)
  $dateFormat = "d F"; // dateFormat = 12 December
else
  $dateFormat = "d F Y"; // dateFormat = 12 December 2009
// now use $dateFormat when displaying the date!

Hope this gives you some ideas.

I entered this in my PHPMYadmin:

2009-12-19 12:15:00

but am not sure what to do to declare the variable properly.

$now = strtotime("NOW"); // timestamp of current date/time
$currentYear = date("Y", $now); // format of "Y" gives four digit year ie 2009 not 09
if($yearOfDateFromDatabase == $currentYear)
  $dateFormat = "d F"; // dateFormat = 12 December
else
  $dateFormat = "d F Y"; // dateFormat = 12 December 2009
// now use $dateFormat when displaying the date!

I'm unsure how to do this for dates and times stored in my database using the DATETIME function.

What variable will I need to declare $yearofDatabase?

Thanks for your help so far!

I would do this all in PHP code rather than trying to mix with PHPMyAdmin. For example,

// assumes that you have connected successfully to your database using mysql_connect and mysql_select_db
$result = mysql_query("select DATE_COLUMN from THE_TABLE"); // runs a query in your database
if($result)
{
   $row = mysql_fetch_assoc($result); // gets the first result set row from the database for the query
   $databaseDate = $row["DATE_COLUMN"]; // that is, reference the column name of your table
   $formattedDate = date("Y-m-d G:i:s", strtotime($databaseDate)); // this puts the date into the format "yyyy-mm-dd h24:mm:ss"
}

Have a fiddle with my example and see what you can come up with. If you are still having problems, maybe post some of your code so we can see what you are trying to do a bit better?

Do not use any text date format, it will always be wrong for 90% of the world
numeric timestamps use less storage, are faster on compare, can be output to any user/regional/whatever preference, and can be manipulated easier

a function appears in the manual does not mean it is the most efficient

Here's my code:

<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","PASSWORD"); 
	
//select which database you want to edit
mysql_select_db("tvguide"); 

//select the table
$result = mysql_query("select * from epdata LIMIT 20;");

//grab all the content
while($r=mysql_fetch_array($result))
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
   $programme=$r["programme"];
   $channel=$r["channel"];
   $airdate = $r["airdate"];
   $episode=$r["episode"];
   $setreminder=$r["setreminder"];
echo "<tr><td><b>$programme</b></td><td>showing on $channel</td>";
echo "<td>$airdate</td><td>$episode</td><td>$setreminder</td></tr>";
}
?>

The column "airdate" in table epdata is stored as datetime - is this a possible cause of my mistakes?
darkagn, I'm not sure where to put your code in the above PHP coding of mine but I will try it!

Thanks so far!

If you are taking a limit you should order by the most relevant field, so the 20 most recent, etc are chosen. $result = mysql_query("select * from epdata order by setreminder LIMIT 20;"); , example only, irelevant if the index is the most relevant field
a text date/time is very difficult to order by, and means different things to different people
a timestamp is easier

It's now TIMESTAMP in PHPMYadmin as you suggested ;)
This is the code for the airdate field:

$airdate=$r["airdate"];

and this is what it produces:

2009-12-20 12:30:00

I'm trying to get it as:

December 28th - 12:30pm
12:30pm (on day itself)
January 6th, 2010 - 8:30pm

but so far it hasn't worked. Thanks for your help so far.

The timestamp field is configurable to give the results needed, you have to read the manuals,
I use a smallint in the table, using timestamps to populate the field, and storing the value as a timestamp produce different results, default formatting screws up my implementations, (too lazy to read the mysql manual)
timestamp will be stored as 1234567890 (10 digit numeric)(text datetime to store the same details may be 50-60 bytes)
MYSQL now() php date() produce a 10 digit numeric (currently 1261288923 by the time you read this the last digits will have incremented by the time since posting)
then the text is output as required by per the php date frunction
smaller tables, faster operations, indexes that return correct results (sorting on date fields often produce unexpected results December comes before November ( Apr Aug Dec Feb Jan Jul Jun Mar May Nov Oct Sep)when you sort a text field)
output configurable to user preference, date formats can be stored in the user table
So I prefer this way to having to work out how to work with the default values stored in the timestamp datatype

almostbob makes some good points but I am a bit confused by some of what they have said. They say that they store their timestamp in a SMALLINT field, but a smallint only has a range from -32768 to 32767 which is not big enough to store a 10 digit number. You would need a minimum size of INT to be able to store such a timestamp, and in terms of size this equates to a 4-byte field, the same as a TIMESTAMP or DATE datatype.

Certainly size of your fields should be a factor when designing your database, however this should not be at the expense of ease of use. Sorting of a DATE field will in fact put November before December as it has the format yyyy-mm-dd (ie 2009-12-21 for today). A DATETIME field (which is what the OP was using I believe) acts similarly but is 8 bytes in size due to the fact that it stores the time data as well as the date.

The OP is questioning how to output the DATETIME field (as opposed to a VARCHAR field) in the format that they require. Using a TIMESTAMP still requires a call to date() for output, while a DATETIME can be output in MySQL format immediately or using the date() and strtotime() functions for a different format. However a DATETIME has the added bonus of being a discernable time for a database administrator running a query directly on the table whereas a TIMESTAMP is much more difficult to interpret in its raw form.

I don't believe that almostbob is incorrect in what they are saying, but I think you need a good reason to use a TIMESTAMP as opposed to a DATETIME. Just my 2 cents worth...

The date is now stored as DATETIME format now, but I still can't get it to output all as this format:
December 22nd
January 13th, 2010

What SQL/PHP code should I use on the field "date" - which these are stored under?

Ok, instead of:

$airdate = $r["airdate"];
// ...
echo "$airdate...";

I would do something like this:

$airdate = $r["airdate"];
$now = strtotime("NOW"); // timestamp of current date/time
$currentYear = date("Y", $now); // format of "Y" gives four digit year ie 2009 not 09
$yearOfDateFromDatabase = date("Y", $airdate); // the year of the record
if($yearOfDateFromDatabase == $currentYear)
  $dateFormat = "d F"; // dateFormat = 12 December
else
  $dateFormat = "d F Y"; // dateFormat = 01 January 2010
// now use $dateFormat when displaying the date!
$airdateFormatted = date($dateFormat, strtotime($airdate));
// ...
echo "$airdateFormatted...";

EDIT: Personally I would put the bulk of the above code into a function but that's up to you.

OK, I tried your code and it's semi-functional, here's the link:
http://www.phpsabox1.200u.com/tvguide/index.php

For some reason despite my PHP code here:

<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","PASSWORD"); 
//select which database you want to edit
mysql_select_db("tvguidesite_tvguide"); 
//select the table
$result = mysql_query("select * from epdata1 order by airdate LIMIT 20") or die(mysql_error());
//grab all the content
while($r=mysql_fetch_array($result));
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
   $programme=$r["programme"];
   $channel=$r["channel"];
$airdate = $r["airdate"];
$now = strtotime("NOW"); // timestamp of current date/time
$currentYear = date("Y", $now); // format of "Y" gives four digit year ie 2009 not 09
$yearOfDateFromDatabase = date("Y", $airdate); // the year of the record
if($yearOfDateFromDatabase == $currentYear)
  $dateFormat = "d F"; // dateFormat = 12 December
else
  $dateFormat = "d F Y"; // dateFormat = 01 January 2010
// now use $dateFormat when displaying the date!
$airdateFormatted = date($dateFormat, strtotime($airdate));
//
   $episode=$r["episode"];
   $setreminder=$r["setreminder"];
echo "<tr><td><b>$programme</b></td><td>showing on $channel</td>";
echo "<td>$airdate</td><td>$episode</td><td>$setreminder</td></tr>";
}
?>

it only says "showing on" and doesn't show database content - what could be causing this?

Make sure your MySQL column names are correctly spelt, remember that it is case sensitive so "programme" is not the same as "PROGRAMME" or "Programme". I would double check these first, if that doesn't work try a print_r($r) at the top of your loop to check whether the row is correct.

I almost figured it out with this code I got from Google:

$airdate = strtotime($r['airdate']);
   $sDate = date("F dS, Y - g:ia",$airdate);

However, I don't want airdate to show the current year (or the comma and the Y in the PHP code above), only for next year's airings.

What should I do?

Combine it with the code I already gave you. The only difference between the code you have there and my code is the date format. Read up on the date function (see the link in my first post in this thread) and have a play around with the date format.

OK, almost working...

//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","PASSWORD"); 
//select which database you want to edit
mysql_select_db("tvguidesite_tvguide"); 
//select the table
$result = mysql_query("select * from epdata1 order by airdate LIMIT 20") or die(mysql_error());

//grab all the content
while($r=mysql_fetch_array($result))
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
   $programme=$r["programme"];
   $channel=$r["channel"];
   #$airdate = strtotime($r['airdate']);
   $airdate=$r["airdate"];
   #
$now = strtotime($r["airdate"]); // timestamp of current date/time
$currentYear = date("Y", $now); // format of "Y" gives four digit year ie 2009 not 09
$yearOfDateFromDatabase = date("Y", $airdate); // the year of the record
if($yearOfDateFromDatabase == $currentYear)
$dateFormat = "nS F - g:ia"; // dateFormat = 12 December
else
$dateFormat = "nS F, Y - g:ia"; // dateFormat = 01 January 2010
// now use $dateFormat when displaying the date!
$airdateFormatted = date($dateFormat, strtotime($airdate));
   $sDate = date("F dS, Y - g:ia",$airdate);
   $episode=$r["episode"];
   $setreminder=$r["setreminder"];
echo "<tr><td><b>$programme</b></td><td>showing on $channel</td>";
echo "<td>$airdateFormatted</td><td>$episode</td><td>$setreminder</td></tr>";
}
?>

I'm getting these errors:

Notice: A non well formed numeric value encountered in C:\www\vhosts\tvguide\tvguide-echo.php on line X

(number not given since it's a Crimson Editor line-number)

Also, bizarrely, I get 12rd December and 1th January, when my database has 23rd December and 1st January.

What should I do now?

thanks for your help!

Sorry, you should use:

$airdate = strtotime($r['airdate']);
$now = strtotime("NOW");

and

$airdateFormatted = date($dateFormat, $airdate);

Which line gives the error that you mentioned?

This is the new code:

//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","PASSWORD"); 
//select which database you want to edit
mysql_select_db("tvguidesite_tvguide"); 
//select the table
$result = mysql_query("select * from epdata1 order by airdate LIMIT 20") or die(mysql_error());

//grab all the content
while($r=mysql_fetch_array($result))
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
   $programme=$r["programme"];
   $channel=$r["channel"];
   #$airdate = strtotime($r['airdate']);
      $airdate = strtotime($r['airdate']);
      $now = strtotime("NOW");
$currentYear = date("Y", "NOW"); // format of "Y" gives four digit year ie 2009 not 09
$yearOfDateFromDatabase = date("Y", $airdate); // the year of the record
if($yearOfDateFromDatabase == $currentYear)
$dateFormat = "nS F - g:ia"; // dateFormat = 24 December
else
$dateFormat = "nS F, Y - g:ia"; // dateFormat = 01 January 2010
// now use $dateFormat when displaying the date!
$airdateFormatted = date($dateFormat, $airdate);
   $sDate = date("F dS, Y - g:ia",$airdate);
   $episode=$r["episode"];
   $setreminder=$r["setreminder"];
echo "<tr><td><b>$programme</b></td><td>showing on $channel</td>";
echo "<td>$airdateFormatted</td><td>$episode</td><td>$setreminder</td></tr>";
}
?>

I get this error:

TVProgramme showing on NBC 12rd December, 2009 - 12:55am "Test" Set Reminder
TVProgramme showing on ITV1 1th January, 2010 - 11:02am "Episode 2" Set Reminder

and

Warning: date() expects parameter 2 to be long, string given in C:\www\vhosts\tvguide\tvguide-echo.phpon line 22

(line 22 is Crimson Editor's numbering system, not here's!)

Hope this helps.

This is my working code:

//select the table
$result = mysql_query("select * from epdata1 LIMIT 20;");

//grab all the content
while($r=mysql_fetch_array($result))
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
   $programme=$r["programme"];
   $channel=$r["channel"];
   #$airdate = strtotime($r['airdate']);
      $airdate = strtotime($r['airdate']);
      $now = strtotime("NOW");
$currentYear = date("Y", $airdate); // format of "Y" gives four digit year ie 2009 not 09
$yearOfDateFromDatabase = date("Y"); // the year of the record
if($yearOfDateFromDatabase == $currentYear)
$dateFormat = "F jS - g:ia"; // dateFormat = 24 December
else
$dateFormat = "F jS, Y - g:ia"; // dateFormat = 01 January 2010
// now use $dateFormat when displaying the date!
$airdateFormatted = date($dateFormat, $airdate);
   $sDate = date("F dS, Y - g:ia",$airdate);
   $episode=$r["episode"];
   $setreminder=$r["setreminder"];
echo "<tr><td><b>$programme</b></td><td>showing on $channel</td>";
echo "<td>$airdateFormatted</td><td>$episode</td><td>$setreminder</td></tr>";
}
?>

However, just one small problem to fix - how do I ensure that it only shows

$dateFormat = "g:ia"; // dateFormat = 24 December
else

(i.e. the airtime) on the day itself using the if statement?

Thanks for all your help.

Again, use the date() function on $airdate and $now to work out the current day and month, like we did with the year but using different date formats rather than "Y" as the comparison.

EDIT: Also, your lines:

$currentYear = date("Y", $airdate);
$yearOfDateFromDatabase = date("Y");

should be:

$currentYear = date("Y", $now);
$yearOfDateFromDatabase = date("Y", $airdate);

Thank you, I will try to work that out - well, here's one copy of my code I made earlier:

//grab all the content
while($r=mysql_fetch_array($result))
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
   $programme=$r["programme"];
   $channel=$r["channel"];
   #$airdate = strtotime($r['airdate']);
      $airdate = strtotime($r['airdate']);
      $now = strtotime("NOW");
$currentTime = date("F jS - g:ia", $now);      
$currentYear = date("Y", $now);
$AirtimeFromFromDatabase = date("F jS - g:ia", $airdate);
$yearOfDateFromDatabase = date("Y", $airdate);
if($yearOfDateFromDatabase == $currentTime)
if($yearOfDateFromDatabase == $currentYear)
$dateFormat = "F jS - g:ia"; // dateFormat = 24 December
else
$dateFormat = "F jS, Y - g:ia"; // dateFormat = 01 January 2010
$currentTime = date("g:ia", $airdate); // format of "Y" gives four digit year ie 2009 not 09
$airdateFormatted = date($dateFormat, $airdate);
   $sDate = date("F dS, Y - g:ia",$airdate);
   $episode=$r["episode"];
   $setreminder=$r["setreminder"];
echo "<tr><td><b>$programme</b></td><td>showing on $channel</td>";
echo "<td>$airdateFormatted</td><td>$episode</td><td>$setreminder</td></tr>";
}

Again, use the date() function on $airdate and $now to work out the current day and month, like we did with the year but using different date formats rather than "Y" as the comparison.

I have done so above but I get this error message:

Notice: Undefined variable: dateFormat in C:\www\vhosts\tvguide.com\tvguide3.php on line number X

(line number being Crimson Editor, rather than here's)

How should I fix it so I can get it to do this:

$dateFormat = "g:ia"; // dateFormat = 24 December
else

- show the airtime on the day itself using the if statement? Above, I ended up getting error messages, hopefully I'll get it soon!

I made a slight change to the database - airdate just shows the date the programme airs, and a new field called "airtime" has been added for the time the programme airs.

How would I modify the code to suit this change? (note the original airtime field is renamed airtime0)

Here's an attempted rewrite:

<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","PASSWORD"); 
	
//select which database you want to edit
mysql_select_db("tvguide"); 

//select the table
$result = mysql_query("select * from epdata3 order by airdate1, airtime1 LIMIT 20;");

//grab all the content
while($r=mysql_fetch_array($result)) or die(mysql_error());
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
   $programme=$r["programme"];
   $channel=$r["channel"];
   #$airdate11 = strtotime($r['airdate1']);
      $airdate11 = strtotime($r['airdate1']);
      $now = strtotime("NOW");
$currentYear = date("Y", $airdate11); // format of "Y" gives four digit year ie 2009 not 09
$yearOfDateFromDatabase = date("Y"); // the year of the record
if($yearOfDateFromDatabase == $currentYear)
$dateFormat = "F jS - g:ia"; // dateFormat = 24 December
else 
$dateFormat = "F jS, Y - g:ia"; // dateFormat = 01 January 2010
else 
	
$currentTime = date("g:ia", $airdate1); // format of "Y" gives four digit year ie 2009 not 09
$airdate1Formatted = date($dateFormat, $airdate1);
   $sDate = date("F dS, Y - g:ia",$airdate1);
   $episode=$r["episode"];
   $setreminder=$r["setreminder"];
echo "<tr><td><b>$programme</b></td><td>showing on $channel</td>";
echo "<td>$airdate1Formatted</td><td>$episode</td><td>$setreminder</td></tr>";
}
?>

However, this always generates this error:

Parse error: syntax error, unexpected T_ELSE in C:\www\vhosts\tvguide.com\tvguide-echodata.php on line 28

Disclaimer: Line 28 in this case refers to Crimson Editor, and not the software here.

Any help would be appreciated on fixing this, since I've renamed some tables and made a copy of the database for the above script.

Thanks

You have one too many "else" lines in your if-statement. I would not have split airdate into separate date and time fields, what is your thinking there?

You have one too many "else" lines in your if-statement. I would not have split airdate into separate date and time fields, what is your thinking there?

I've now reversed that - airdate is now DATETIME.
However, what I am trying to do is get it to show it as g:ia on today's date for all programmes airing, the code is now:

//select the table
$result = mysql_query("select * from epdata3 order by airdate LIMIT 20;");

//grab all the content
while($r=mysql_fetch_array($result))
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
   $programme=$r["programme"];
   $channel=$r["channel"];
   #$airdate = strtotime($r['airdate']);
      $airdate = strtotime($r['airdate']);
      $now = strtotime("NOW");
$currentYear = date("Y", $now); // format of "Y" gives four digit year ie 2009 not 09
$yearOfDateFromDatabase = date("Y", $airdate);
if($yearOfDateFromDatabase == $currentYear)
$dateFormat = "F jS - g:ia"; // dateFormat = 24 December
else
$dateFormat = "F jS, Y - g:ia"; // dateFormat = 01 January 2010
$currentTime = date("g:ia", $airdate); 
$airdateFormatted = date($dateFormat, $airdate);
   $sDate = date("F dS, Y - g:ia",$airdate);
   $episode=$r["episode"];
   $setreminder=$r["setreminder"];
echo "<tr><td><b>$programme</b></td><td>showing on $channel</td>";
echo "<td>$airdateFormatted</td><td>$episode</td><td>$setreminder</td></tr>";
}
?>

I can get date/time to render properly with no problems, it's just for today that I want it to display the time only (or g:ia in php date function).

Thanks for your help so far.

Your algorithm should be:
- is it in the current year?
- NO - DateFormat = F jS, Y - g:ia
- YES
-- Is it on the current day?
-- NO - DateFormat = F jS - g:ia
-- YES - DateFormat = g:ia

EDIT: I have already shown you how to find the current year, finding the current day is the same except that you need day and month in your format of the current date.

Your algorithm should be:
- is it in the current year?
- NO - DateFormat = F jS, Y - g:ia
- YES
-- Is it on the current day?
-- NO - DateFormat = F jS - g:ia
-- YES - DateFormat = g:ia

EDIT: I have already shown you how to find the current year, finding the current day is the same except that you need day and month in your format of the current date.

OK, I'm almost getting there... here's the code using what you said above:

//select the table
$result = mysql_query("select * from epdata3 order by airdate LIMIT 20;");

//grab all the content
while($r=mysql_fetch_array($result))
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
   $programme=$r["programme"];
   $channel=$r["channel"];
   #$airdate = strtotime($r['airdate']);
      $airdate = strtotime($r['airdate']);
      $now = strtotime("NOW");
$currentYear = date("Y", $now);
$yearOfDateFromDatabase = date("Y", $airdate);
if($yearOfDateFromDatabase == $currentYear)
$dateFormat = "F jS - g:ia"; // dateFormat = 24 December
else
$dateFormat = "F jS, Y - g:ia"; // dateFormat = 01 January 2010
$now = strtotime("NOW"); // timestamp of current date/time
$currentDay = date("Y", $now); // format of "Y" gives four digit year ie 2009 not 09
if($currentDay == $currentYear)
  $currentDay = "F jS - g:ia"; // dateFormat = 12 December
else
  $currentDay = "F jS, Y - g:ia"; // dateFormat = 12 December 2009
$currentTime = date("Y", $now); // format of "Y" gives four digit year ie 2009 not 09
if($currentTime == $currentDay)
  $currentTime = "g:ia"; // dateFormat = 12 December
else
  $currentTime = "F jS - g:ia"; // dateFormat = 12 December 2009  
// now use $dateFormat when displaying the date!
$currentDayFormatted = date($currentDay, $currentTime);
   $sDate = date("F dS, Y - g:ia",$airdate);
   $episode=$r["episode"];
   $setreminder=$r["setreminder"];
echo "<tr><td><b>$programme</b></td><td>showing on $channel</td>";
echo "<td>$currentDayFormatted</td><td>$episode</td><td>$setreminder</td></tr>";
}
?>

but I get this error:

Warning: date() expects parameter 2 to be long, string given in C:\www\vhosts\tvguide.com\tvguide-echodata.php on line 40

Warning: date() expects parameter 2 to be long, string given in C:\www\vhosts\tvguide.com\tvguide-echodata.php on line 40

(again, using Crimson Editor, so the line 40 referred to will vary between editors).

Thank you for your help so far. :)

It's slowly getting there, I'm using your advice on my other website project too.

$currentDayFormatted = date($currentDay, $currentTime);

should be:

$currentDayFormatted = date(<whatever date format you meant>, strtotime($currentTime));

EDIT: In future, please do not say "using Crimson Editor, so the line XX referred to will vary between editors", instead just tell me which line is the line that the error refers to.

$currentDayFormatted = date($currentDay, $currentTime);

should be:

$currentDayFormatted = date(<whatever date format you meant>, strtotime($currentTime));

EDIT: In future, please do not say "using Crimson Editor, so the line XX referred to will vary between editors", instead just tell me which line is the line that the error refers to.

I fixed it a bit, but now it displays January 1st - 1:00am for all records, even though the dates are not January 1st.

This is the code:

//modify these to match your mysql table columns
   $programme=$r["programme"];
   $channel=$r["channel"];
   #$airdate = strtotime($r['airdate']);
      $airdate = strtotime($r['airdate']);
      $now = strtotime("NOW");
$currentYear = date("Y", $now);
$yearOfDateFromDatabase = date("Y", $airdate);
if($yearOfDateFromDatabase == $currentYear)
$dateFormat = "F jS - g:ia"; // dateFormat = 24 December
else
$dateFormat = "F jS, Y - g:ia"; // dateFormat = 01 January 2010
$now = strtotime("NOW"); // timestamp of current date/time
$currentDay = date("Y", $now); // format of "Y" gives four digit year ie 2009 not 09
if($currentDay == $currentYear)
  $currentDay = "F jS - g:ia"; // dateFormat = 12 December
else
  $currentDay = "F jS, Y - g:ia"; // dateFormat = 12 December 2009
$currentTime = date("Y", $now); // format of "Y" gives four digit year ie 2009 not 09
if($currentTime == $currentDay)
  $currentTime = "g:ia"; // dateFormat = 12 December
else
  $currentTime = "F jS - g:ia"; // dateFormat = 12 December 2009  
// now use $dateFormat when displaying the date!
$currentDayFormatted = date($currentDay, strtotime($currentTime));
   $sDate = date("F dS, Y - g:ia",$airdate);
   $episode=$r["episode"];
   $setreminder=$r["setreminder"];
echo "<tr><td><b>$programme</b></td><td>showing on $channel</td>";
echo "<td>$currentDayFormatted</td><td>$episode</td><td>$setreminder</td></tr>";
}

I'm not sure how to get the variables fixed properly though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.