This is my PHP/MySQL query, as mentioned at Displaying links in PHP/MySQL?:
http://pastebin.com/5Td9Bybn
I\'ll admit, I\'ve forgotten how to use the strtotime()
You have a space between the <?
and the word php. I think that is causing the unexpected echo error. You should have <?php
The PHP function strtotime() has the following usage:
int strtotime ( string $time [, int $now ] )
This means that you pass in a string value for the time, and optionally a value for the current time, which is a UNIX timestamp. The value that is returned is an integer which is a UNIX timestamp.
An example of this usage is as follows, where the date passed to strtotime() might be a date from a database query or similar:
$ts = strtotime('2007-12-21');
This will return into the $ts variable the value 1198148400, which is the UNIX timestamp for the date December 21st 2007. This can be confirmed using the date() function like so:
echo date('Y-m-d', 1198148400);
// echos 2007-12-21
strtotime() is able to parse a wide variety of strings and convert them to the appropriate timestamp, using actual dates and also strings such as "next week", "next tuesday", "last thursday", "2 weeks ago" and so on. Here are some examples:
$ts = strtotime('21 december 2007');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';
This will display the following:
1198148400 2007-12-21
If today is December 21st, then the following:
$ts = strtotime('next week');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';
$ts = strtotime('next tuesday');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';
$ts = strtotime('last thursday');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';
$ts = strtotime('2 weeks ago');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';
$ts = strtotime('+ 1 month');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';
will display the following:
1199006542
2007-12-30
1198494000
2007-12-25
1198062000
2007-12-20
1197192142
2007-12-09
1201080142
2008-01-23
You can do it directly in MySQL, which saves you the overhead of strtotime()'s parseing overhead, and guaranteed to produce the right results, as strtotime can guess wrong occasionally.
SELECT DATE_FORMAT(yourdatefield, '%h:%i') ...
strtotime()
will give you a timestamp, you use the function date() to format the display of time and/or date.
06:00
<?php echo date('H:i', strtotime($result_ar['airtime'])); ?>
6:00am
<?php echo date('g:ia', strtotime($result_ar['airtime'])); ?>
6am
<?php echo date('ga', strtotime($result_ar['airtime'])); ?>
Read about date() and its formatting on php.net.
Do it in MySQL, as @Marc B suggests.
Change this line
$result = mysql_query("SELECT * FROM presenters1;", $connection) or die("error querying database");
to
$result = mysql_query("SELECT *, TIME_FORMAT(airtime, '%h:%i') `airtime` FROM presenters1;", $connection) or die("error querying database");
Then, change this line:
<div class="time"><? php echo strotime ('H') $result_ar['airtime']; ?></div>
to
<div class="time"><?php echo $result_ar['airtime']; ?></div>