Using the time to retrieve specific content from a database through php

亡梦爱人 提交于 2019-12-25 18:42:47

问题


Am trying to display information on a website based on the local time. Each element has 3 elements.

1. A start time from 01 - 24
2. An end time from 00 - 60
3. A day of the week from Mon - Sun

What I want to do is load a specific element from the database, based on the day and time. How do I go about it? Is there any scripts to do this in php/mysql?

I've managed to get all the elements from the database to load based on the day, using the script below;

<? 

$day = date('l');  // weekday name (lower-case L) 
$time = (int)date("Gi");

$getdays = mysql_query("SELECT * FROM pages WHERE title = '".$day."'"); $getdays = mysql_fetch_array($getdays);
$getonair = mysql_query("SELECT * FROM pages WHERE subnav LIKE '%".$getdays['id']."%' AND type = '10'"); 

while ($goa = mysql_fetch_array($getonair)) {   

?><? echo $goa['id']; ?> - <? echo $goa['content']; ?></div>
<? } ?>

This currently will display all the elements in the database for today with their start and end times i.e.

item 1
start time: 1700
end time: 1730
content: some content here

item 2
start time: 1400
end time: 1430
content: some content here

item 3
start time: 0400
end time: 1430
content: some content here

item 4
start time: 1800
end time: 1830
content: some content here

item 5
start time: 2200
end time: 2230
content: some content here

What I now need to do is figure out a way to select the right item and display it based on the current time. How do I do this???


回答1:


OK after cracking my brains and going through some articles, I figured out a way to make it work and it's quite simple. Check the query below for more details.

<? 

$day = date('l');  // weekday name (lower-case L) 
$time = (int)date("Gi");

$getdays = mysql_query("SELECT * FROM pages WHERE title = '".$day."'"); $getdays =   
mysql_fetch_array($getdays);
$getonair = mysql_query("SELECT * FROM pages WHERE subnav LIKE '%".$getdays['id']."%'
`content` <= '".$time."' AND `caption` >= '".$time."' AND type = '10'"); 

while ($goa = mysql_fetch_array($getonair)) {   

?><? echo $goa['id']; ?> - <? echo $goa['content']; ?></div>
<? } ?>


来源:https://stackoverflow.com/questions/16218517/using-the-time-to-retrieve-specific-content-from-a-database-through-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!