I now get 1 error of all dates being 1st January - 1:00am for the entries which is odd.

The code is as mentioned above.

My suggestion would be to read the documentation of the date function, as I have said before in this thread (twice). Your calls to date do not make sense and I suspect that this is the problem. For example:

$currentDay = date("Y", $now);

This will give the variable $currentDay the value "2009". Is this really what you want? If so the name of the variable does not make sense to me.

Also, please stop PM'ing me, I will look at the thread when I get time.

OK, here's the modified 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("g:ia", $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 attempting to get it so that when it's the current day it just shows the time, and it shows it in these formats:

6:00pm (current day - which I can't get it to show, it always displays as 1st January 1970 - 1:00am due to me being unsure of what the variable should be!)
January 3rd - 6:00pm (date and time, which I got working before)
January 3rd, 2011 - 6:00pm (date and time, as per above)

Apologies for my mistake.

Ok, I think you've got yourself confused with all the variables.
When I gave you this code:

$airdate = strtotime($r['airdate']);
$now = strtotime("NOW");
$currentYear = date("Y", $now);
$yearOfDateFromDatabase = date("Y", $airdate);
if($yearOfDateFromDatabase == $currentYear)

this is what I was doing:
-- get the timestamp of the database record ($airdate)
-- get the current timestamp ($now)
-- calculate the current year ($currentYear)
-- determine the year of the database record ($yearOfDateFromDatabase)
-- compare the current year to the year of the database record

You need to repeat this algorithm for the current day. Here is how I would find the current day:

$currentDay = date("d m", $now); // $now is already defined as the current timestamp

This will make $currentDay the following string for today's date (in Australia anyway): "31 12".
So then you will need to use $airdate to find the day of the database record and compare it to $currentDay.

As an aside, the date 1st January, 1970 at midnight is known as the Unix Epoch. PHP Timestamps are the number of seconds since then, so if you see that date in your output it generally means that your date function has been called with a very low timestamp (the second parameter in the call).

Man, this thread is making my head hurt!
The long and short of it comes down to:
MySQL has it's own date time format. There is no way to change it.
PHP has all of those functions just for such occasions.
You can use strftime() to format the date the way you wish to display it, but only save it the way that MySQL wants it. Once again you can use strftime() to change it back to MySql format.
The only drawback is that Strftime() will call time() if it gets a null argument for time.
I would have preferred that it just showed a blank...

commented: all good points :) +3

Hi JRM,

Sorry for the swings and round abouts, I was trying to explain what was happening without just handing the answer over so that whitestream6 could learn from what was being done. strftime is indeed another approach, but I started down the date() path and didn't want to confuse whitestream6 further. But you do make good points, thanks for the contribution :)

Thanks for your help, the code's working well so far - but trying to get it as g:ia for the current day is the only thing not working (no error messages, just my lack of understanding!)

//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
$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>";
}
?>

Trying to use the ifelse statements caused error messages so I just reverted it back to the above code - I'm not sure exactly how to get it so tried this experiment:

//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
//
$currentTime = date("g:ia", $airdate); // format of "Y" gives four digit year ie 2009 not 09
if($currentTime == $currentYear)
$dateFormat = "g:ia"; // dateFormat = 24 December
else
$dateFormat = "F jS - g:ia"; // dateFormat = 24 December
else
$dateFormat = "F jS, Y - g:ia"; // dateFormat = 01 January 2010
$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>";
}
?>

but this didn't work... anyone know how to get it so that on the current date, it's formatted like this:

if($currentTime == $currentYear)
$dateFormat = "g:ia"; // dateFormat = 24 December
else
$dateFormat = "F jS - g:ia"; // dateFormat = 24 December
else
$dateFormat = "F jS, Y - g:ia"; // dateFormat = 01 January 2010

The earlier code worked well - but it's this bit that is proving tricky since I don't know what the best variables to use with date() are.

$airdate is stored in DATETIME format.
Basically, what I'm trying to do is strip the first part (i.e. the one that shows the date) and show just the time on the day itself, i.e. today
Example:

December 31st - 9:00pm

and how it should be (on the date itself):

9:00pm

I'm confused too.

Oh, and Happy New Year / 2010 to you!

Your if-statement has too many else's. An if-statement in PHP looks like this:

if(boolean condition 1)
{
  // do something
}
else if(boolean condition 2)
{
  // do something else
}
...
else if(boolean condition X)
{
  // do something else
}
else
{
  // if we get here then ALL previous conditions have evaluated to false
  // so there can only ever be one "else" but as many "else if" statements as you need
}

$airdate is ok because when you have grabbed it from the database you have passed it through a call to strtotime to change it to a timestamp.

EDIT: whitestream6, you are very close. Your problem lies not in your calls to date() but in how you are assessing when to use which date format.

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.