用PHP编写的电视指南 - datetime()和数据库函数的问题 [英] TV guide written in PHP - problems with datetime() and database functions

查看:161
本文介绍了用PHP编写的电视指南 - datetime()和数据库函数的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个电视指南,其中列出了所有数据存储在数据库中的节目(以及一些列表中的过去的节目)。它运行在PHP,我的版本是5.28(升级到5.30或6很快)。

I'm creating a TV Guide which lists programmes coming up (and on some listings, previous airings from the past), with all data stored in a database. It runs in PHP, my version being 5.28 (upgrading to 5.30 or 6 soon).

下面是一个脚本工作(注意字段airdate存储为DATETIME在数据库):

Below is a script which works (note the field airdate is stored as DATETIME in the database):

[免责声明:脚本不是我的,而是一个通用的,我下载,并修改以适应我自己的需要。]

[Disclaimer: The script isn't mine, but a generic one I downloaded, and modified to suit my own needs.]

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

显示所有的剧集,如果下一年有新的剧集,如下:

That displays all the episodes coming up, and if there's any coming up the next year, it displays them with the year, like this:


电视节目在2009年12月30日 - 下午6:00播出下一个节目第1集 - 光合作用设置提醒


电视节目在频道1上显示下一个1月6日 - 下午2:45第2集 - 背后音乐设置提醒

电视节目在1号频道1月7日 - 8:00 终极车祸提醒

TV Programme showing next on Channel1 December 30th, 2009 - 6:00pm "Episode 1 - Photosynthesis" Set Reminder
TV Programme showing next on Channel1 January 6th - 2:45pm "Episode 2 - Behind the Music" Set Reminder
TV Programme showing next on Channel1 January 7th - 8:00pm "Ultimate Car Crimes" Set Reminder

但是,我想做的是在一段时间过期后删除某些记录但是必须在脚本中的某处设置,因为程序长度不同),而不是从数据库手动删除它们。一些程序是30分钟长,其他60分钟...长度各不相同,基本上。

However, what I would like it to do is remove certain records after a period of time has expired (but that would have to be set somewhere in the script, since programme lengths vary) rather than me manually deleting them from the database. Some programmes are 30 minutes long, others 60 minutes... lengths vary, basically.

我想要做的是这里(注意第一个列表不显示日期,因为它是当前日期。):

What I would like it to do is this (notice that the first listing does not show the date as it is the current date.):


电视节目下一个在Channel1下显示6:00 pmCCTV Cities - Wigan设置提醒


电视节目在Channel1上显示下一个1月9日 - 2:45罗马帝国 - 其人民的历史设置提醒


电视节目在频道1上显示下一个1月10日 - 8:00 pm名人100最差时刻设置提醒
TV Programme showing next on Channel1 6:00pm "CCTV Cities - Wigan" Set Reminder
TV Programme showing next on Channel1 January 9th - 2:45pm "Roman Empire - A History of its People" Set Reminder
TV Programme showing next on Channel1 January 10th - 8:00pm "Celebrity 100 Worst Moments" Set Reminder

但我不知道如何配置它使用PHP或date()函数。它与日期工作正常,并显示他们。
我无法访问cron作业,因为这是在Windows Vista Home Edition上的localhost Apache安装。

but I don't know how to configure it to do this with PHP or the date() function. It works fine with the dates, and showing them. I don't have access to cron jobs since this is on a localhost Apache installation on Windows Vista Home Edition.

如果任何人可以帮助我非常感谢 - 所有的帮助是非常感激。

If anyone could help me figure this out it would be much appreciated - all help is much appreciated.

我没有把这个作为一个活网站,因为它是发展地狱,现在,我

I haven't put this as a live site, since it's "in development hell" right now, and I want to get things right as much as possible.

推荐答案

function reldate ($time) {
    $now = time();
    $cmp_fmt = '%Y%m%d';
    if (strftime($cmp_fmt, $time) == strftime($cmp_fmt, $now)) {
        $out_fmt = '%I:%M %P';
    } else {
        $day = strftime('%e', $time);
        if (preg_match('/([^1]1|^1)$/', $day)) {
            $day_suffix = 'st';
        } elseif (preg_match('/([^1]2|^2)$/', $day)) {
            $day_suffix = 'nd';
        } elseif (preg_match('/([^1]3|^3)$/', $day)) {
            $day_suffix = 'rd';
        } else {
            $day_suffix = 'th';
        }
        $out_fmt = '%B %e' . $day_suffix . ' - %I:%M %P';
    }

    return strftime($out_fmt, $time);
}

这篇关于用PHP编写的电视指南 - datetime()和数据库函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆