I'm trying to display some content based on a hour.
This hour is coming from DB as string field.
If the current time match or passed the time from DB, a certain content should be displayed else it's the default content
What i've tried so far :
$time = strtotime($p_objetct->getHour());
//we check if this has already passed to
//retrieve corresponding content else we display default content
if ($time >= date("h:i:sa")) {
echo 'time to display new content !';
}else{
echo 'display content default';
}
This code is not working well as you can guess 'cause if I want to display new content at 20:00:00, the 1st condition is true when the current time is 08:00:00..
I've also tried that
$time = strtotime(p_objetct->getHour());
$selection = floor((date('G',$time) / 2));
switch ($selection) {
case 0:
echo 'time between 0-2!';
break;
case 1:
echo 'time between 2-4!';
break;
case 2:
echo 'time between 4-6!';
break;
case 3:
echo 'time between 6-8!';
break;
case 4:
echo 'time between 8-10!';
break;
case 5:
echo 'time between 10-12!';
break;
case 6:
echo 'time between 12-14!';
break;
case 7:
echo 'time between 14-16!';
break;
case 8:
echo 'time between 16-18!';
break;
case 9:
echo 'time between 18-20!';
break;
case 10:
echo 'time between 20-22!';
break;
case 11:
echo 'time between 22-24!';
break;
}
But this wasnt the good approach because i cant guess in which case the time from DB will be..
Any ideas are welcomed !
Edit :
I have 2 contents (new and default).
The new content should be display from time from DB until 00:00:00 then it should be the default one
If you get only the hour in 24h format from your $p_objetct->getHour() method you can simply compare it to date('H');.
If you wat exact matches you can use:
<?php
$hourToCheck = $p_objetct->getHour();
$currentHour = date('H'); // As of PHP manual "H" returns the hour in 24h format
$contentToDisplay = "What ever you wanna display as default.";
if ($hourToCheck === $currentHour) {
$contentToDisplay = "Your matching content.";
}
If you need it as a lower bound time feel free to use and change this snippet:
<?php
$hourToCheck = $p_objetct->getHour();
$currentHour = date('H'); // As of PHP manual "H" returns the hour in 24h format
$contentToDisplay = "What ever you wanna display as default.";
if ($hourToCheck <= $currentHour) {
$contentToDisplay = "Your matching content 1.";
}
You could try to set a variable with a name based on time, matching an existing file with that name and require it with php. Like this:
<?php
$time = date('H',time());
$name_of_content_to_require = false;
switch ($time) {
case 0:
$name_of_content_to_require = '0-2';
break;
case 1:
$name_of_content_to_require = '2-4';
break;
case 2:
$name_of_content_to_require = '4-6';
break;
case 3:
$name_of_content_to_require = '6-8';
break;
case 4:
$name_of_content_to_require = '8-10';
break;
case 5:
$name_of_content_to_require = '10-12';
break;
case 6:
$name_of_content_to_require = '12-14';
break;
case 7:
$name_of_content_to_require = '14-16';
break;
case 8:
$name_of_content_to_require = '16-18';
break;
case 9:
$name_of_content_to_require = '18-20';
break;
case 10:
$name_of_content_to_require = '20-22';
break;
case 11:
$name_of_content_to_require = '22-24';
break;
}
if( $name_of_content_to_require ) {
require_once __DIR__ . $name_of_content_to_require . '.php';
} else {
die('with some error, or throw an exception');
}
Related
PHP Script Snippet (only one holiday):
<?PHP
function calculateBankHolidays($yr) {
$bankHols = Array();
// New year's:
switch ( date("w", strtotime("01-01-$yr 12:00:00")) ) {
case 6:
$bankHols[] = "03-01-$yr";
break;
case 0:
$bankHols[] = "02-01-$yr";
break;
default:
$bankHols[] = "01-01-$yr";
}
// Good friday:
$bankHols[] = date("d-m-y", strtotime( "+".(easter_days($yr) - 2)." days", strtotime("21-03-$yr 12:00:00") ));
// Easter Monday:
$bankHols[] = date("d-m-y", strtotime( "+".(easter_days($yr) + 1)." days", strtotime("21-03-$yr 12:00:00") ));
// May Day:
if ($yr == 1995) {
$bankHols[] = "08-05-1995"; // VE day 50th anniversary year exception
} else {
switch (date("w", strtotime("01-05-$yr 12:00:00"))) {
case 0:
$bankHols[] = "02-05-$yr";
break;
case 1:
$bankHols[] = "01-05-$yr";
break;
case 2:
$bankHols[] = "07-05-$yr";
break;
case 3:
$bankHols[] = "06-05-$yr";
break;
case 4:
$bankHols[] = "05-05-$yr";
break;
case 5:
$bankHols[] = "04-05-$yr";
break;
case 6:
$bankHols[] = "03-05-$yr";
break;
}
}
return $bankHols;
}
header('Content-Type: application/json');
$bankHolsThisYear = calculateBankHolidays(2017);
echo (json_encode($bankHolsThisYear, JSON_PRETTY_PRINT));
?>
Result:
[
"02-01-2017",
"14-04-17",
"17-04-17",
"01-05-2017",
"2017-05-29",
"2017-08-28",
"2017-12-25",
"2017-12-26"
]
Shows current full scripts results
Desired Outcome:
{
"Holiday Name": {
"Start Date": ,
"End Date": ,
"Holiday type": ,
"Where it is observed": ,
},
Questions:
How do I add a "Holiday Name" to the parent of each value?
How do I add "Start Date" to each current value?
You could create an multidimensional associative array of objects and do something like:
$listOfHolidays=array(
'halloween'=>array('start'=>'10-31','end'=>'10-31','type'=>'trick or treat','celebratedBy'=>'childhood'),
'newYear'=>array('start'=>'12-31','end'=>'01-01','type'=>'new year','celebratedBy'=>'everyone'),
);
echo json_encode($listOfHolidays);
Tested: this is my output:
{
"halloween":
{"start":"10-31",
"end":"10-31",
"type":"trick ortreat",
"celebratedBy":"childhood"
},
"newYear":
{"start":"12-31",
"end":"01-01",
"type":"new year",
"celebratedBy":"everyone"
}
}
EDIT: as you commented about a switch, i'm not sure i understand the precision but you can easily get the 'holidays' by using the associative keys like so:
$boo=$array['halloween'];
And then get the value from this holiday with:
$boo['type']; //trick ortreat
OR you could alternativly get the value straight from the original array:
echo $array['newYear']['end']; //01-01
further more you can also add a value to the array:
$array['newYear']['bonus']='300$';
Also, just a friendly reminder that you can resotre the aray from jason simply by using the TRUE switch in json_decode like so:
$array=json_decode($json,true);
As for the switch, still i don'T see how you could be using a switch unless you loop through the holidays:
foreach($array as $k=>$v){
switch($k){
case 'halloween': echo $v['end']; break; //10-31
case 'newYear': echo $v['bonus']; break; //300$
default: echo 'normal work day'; break;
}
}
Hope this helps.
I am working on a php base online forum Sincerely speaking i bought the script from codecanyon am still a newbie in php the index page contain $_GET nd switch case which will help in navigating to the other pages but its working keep showing page not find. I have tried all i can pls i need your help thanx.....
`<?php
include("includes/db_config.php");
include("includes/google_config.php");
include("includes/functions.php");
include("includes/loaders.php");
//get web settings
$web = mysql_fetch_array(mysql_query("SELECT * FROM settings ORDER BY id
DESC LIMIT 1"));
//update user online time
if($_SESSION['usern']) {
$user_id = userinfo($_SESSION['usern'],"id");
$online_time = time();
$update = mysql_query("UPDATE users SET online_time='$online_time' WHERE
id='$user_id'");
}
//update forum visits
update_visits();
load_header();
$page = protect($_GET['page']);
}
switch($page) {
case "set_password": include("pages/set_password.php"); break;
case "chat_content": include("pages/chat_content.php"); break;
case "chat": include("pages/chat.php"); break;
case "tag": include("pages/tag.php"); break;
case "forum_sign_in": include("pages/sign_in.php"); break;
case "forum_sign_up": include("pages/sign_up.php"); break;
case "forum_lostpassword": include("pages/lostpassword.php"); break;
case "forum_profile": include("pages/profile.php"); break;
case "forum_messages": include("pages/messages.php"); break;
case "forum_online_users": include("pages/online_users.php"); break;
case "forum_adpanel": include("pages/adpanel.php"); break;
case "view_forum": include("pages/view_forum.php"); break;
case "view_thread": include("pages/view_thread.php"); break;
case "post_thread": include("pages/post_thread.php"); break;
case "post_replie": include("pages/post_replie.php"); break;
case "post_edit": include("pages/post_edit.php"); break;
case "post_delete": include("pages/post_delete.php"); break;
case "post_quote": include("pages/post_quote.php"); break;
case "post_report": include("pages/post_report.php"); break;
case "userinfo": include("pages/userinfo.php"); break;
case "search": include("pages/search.php"); break;
case "read_message": include("pages/read_message.php"); break;
case "send_message": include("pages/send_message.php"); break;
case "reply_message": include("pages/reply_message.php"); break;
case "delete_message": include("pages/delete_message.php"); break;
case "panel": include("pages/panel.php"); break;
case "adpanel_func": include("pages/adpanel_func.php"); break;
case "forum_logout":
unset($_SESSION['usern']);
session_destroy();
session_unset();
$redir = $web['forum_url']."sign_in/";
header("Location: $redir");
break;
default: include("pages/home.php");
}
load_footer();
?>
First you have to debug whats inside the $page variable with:
var_dump($page);
and look whats is the value when you click that link.
NOTE:
I see a brace "`" before the php tag opener, delete it
here is my code :
<?php
// content="text/plain; charset=utf-8"
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php');
include("mysql _connect .php");
$code="CSC1113";
$ac_yr="2010/2011";
$sql = "SELECT results, COUNT(*) 'No_Of_grades' FROM std_results WHERE code='$code' && ac_year='$ac_yr' GROUP BY results ORDER BY results ASC";
$result = mysql_query($sql) or die(mysql_error());
while($ors = mysql_fetch_array($result)) {
$grd=$ors['results'];
switch ($grd)
{
case "A":
$datay[1]=$ors['No_Of_grades'];
break;
case "A+":
$datay[0]=$ors['No_Of_grades'];
break;
case "A-":
$datay=$ors['No_Of_grades'];
break;
case "B":
$datay[4]=$ors['No_Of_grades'];
break;
case "B+":
$datay[3]=$ors['No_Of_grades'];
break;
case "B-":
$datay[5]=$ors['No_Of_grades'];
break;
case "C":
$datay[7]=$ors['No_Of_grades'];
break;
case "C+":
$datay[6]=$ors['No_Of_grades'];
break;
case "C-":
$datay[8]=$ors['No_Of_grades'];
break;
case "D":
$datay[10]=$ors['No_Of_grades'];
break;
case "D+":
$datay[9]=$ors['No_Of_grades'];
break;
case "E":
$datay[11]=$ors['No_Of_grades'];
break;
case "AB":
$datay[12]=$ors['No_Of_grades'];
break;
case "NE":
$datay[13]=$ors['No_Of_grades'];
break;
default:
$datay[14]=$ors['No_Of_grades'];
}
}
//set vlaue zero for othe grades..
for($i=0;$i<15;$i++){
if(!isset($datay[$i])){
$datay[$i]=0;
}
}
// Create the graph. These two calls are always required
$graph = new Graph(550,320,'auto');
$graph->SetScale("textlin");
//$theme_class="DefaultTheme";
//$graph->SetTheme(new $theme_class());
// set major and minor tick positions manually
$graph->yaxis->SetTickPositions(array(0,4,8,12,16,20), array(2,6,10,14,18));
$graph->SetBox(false);
//$graph->ygrid->SetColor('gray');
$graph->ygrid->SetFill(false);
$graph->xaxis->SetTickLabels(array('A+','A','A-','B+','B','B-','C+','C','C-','D+','D','E','AB','NE','MC'));
$graph->yaxis->HideLine(false);
$graph->yaxis->HideTicks(false,false);
// Create the bar plots
$b1plot = new BarPlot($datay);
// ...and add it to the graPH
$graph->Add($b1plot);
$b1plot->SetColor("white");
$b1plot->SetFillGradient("#4B0082","white",GRAD_LEFT_REFLECTION);
$b1plot->SetWidth(25);
$graph->title->Set("Bar Gradient(Left reflection)");
// Display the graph
$graph->Stroke();
?>
here jpgrpah doesn't display..error shows : JpGraph Error: 25067 Your manually specified scale and ticks is not correct. The scale seems to be too small to hold any of the specified tick marks.
but I gave direct data for above $datay array.this code is working perfectly..like this
$datay[0]=2;
$datay[1]=5;
$datay[2]=1;
$datay[3]=2;
$datay[4]=0;
$datay[5]=0;
$datay[6]=3;
$datay[7]=0;
$datay[8]=3;
$datay[9]=0;
$datay[10]=1;
$datay[11]=1;
$datay[12]=0;
$datay[13]=1;
$datay[14]=1;
wht's the wrong with my code ....can't understand....help me...thanxx in advanced...
I think now that i understand you :
case "A-":
$datay=$ors['No_Of_grades'];
break;
should be:
case "A-":
$datay['2']=$ors['No_Of_grades'];
break;
I use the class below to route all requests for php on my web application. Can I improve upon this?
/*route*/
class route
{
function __construct($a)
{
if(isset($_FILES['ufile']['tmp_name'])) // handles file uploads
{
new upload();
}
elseif(isset($_POST['a'])) // handles AJAX
{
$b=$_POST['a'];
switch($b)
{
case '0':
new signin();
break;
case '1':
new signup();
break;
case '2':
session::finish();
break;
case '3':
new bookmark('insert');
break;
case '3a':
new bookmark('delete');
break;
case '4':
new tweet();
break;
default:
echo "ajax route not found";
break;
}
}
elseif($a!=0) // handles views
{
new view($a);
}
else
{
// route not found
}
}
}
Verification(passes)
/*ROUTE
// Test Code - create entry
new route(0);
new route(1);
$_FILES['ufile']['tmp_name']='test file';
new route(0);
unset($_FILES['ufile']['tmp_name']);
$_POST['a']=0;
new route(0);
// Test Cases
// Case 0: echo "not routed: <br>";
// Case 1: echo "view created: $a <br>";
// Case 2: echo "file uploaded <br>";
// Case 3: echo "ajax responded: <br>";
*/
public static function route($a)
{
// The first if statement is redundant this line will accomplish the
// same as the if/else because if post[a] is not set it will become null
$b=$_POST["a"];
// now that b is a, it's really one switch statement
if( $b==0 && $a==0 )
switch( $b )
{
case '0':
new signin();
break;
case '1':
new signup();
general::upload();
break;
case '2':
session::finish();
break;
case '3':
new bookmark('insert');
break;
case '3a':
new bookmark('delete');
break;
case '4':
new tweet();
break;
default:
view::posts_all();
break
}
}elseif( $a==1 )
view::bookmarks();
else
view::posts_all();
Give that a go, Good luck. (A side note: the quotation marks on the numeric cases are optional, the 3a is not. I left them in there because they were in the original. You could reduce it further by getting rid of $b entirely and running the switch on $_POST['a'] )
if/else statement lets you set particular condition evaluations, while switch/case only lets you set some particular values that the variable may assume (i.e. in a switch/case you cannot say something like $b > 10).
Except for that, there's no much difference between if/else or switch/case.
I suggest you to you use switch/case construct, since you are just comparing $b with a group of constants.
Besides, remember premature optimization is the root of all evil :)
The first one is definitely something that works, but which one below is the efficient way?
switch($type) {
case 1:
print 'success';
break;
case 2:
print 'success';
break;
case 3:
print 'success';
break;
case 4:
print 'success for type 4';
break;
}
Since 1, 2 and 3 print do the same, can I do this?
switch($type) {
case 1, 2, 3:
print 'success';
break;
case 4:
print 'success for type 4';
break;
}
or
switch($type) {
case 1:
case 2:
case 3:
print 'success';
break;
case 4:
print 'success for type 4';
break;
}
switch($type)
{
case 1:
case 2:
case 3:
print 'success';
break;
case 4:
print 'success for type 4';
break;
}
Is the way to go!
PHP manual lists an example like your 3rd for switch:
<?php
switch ($i) {
case 0:
case 1:
case 2:
echo "i is less than 3 but not negative";
break;
case 3:
echo "i is 3";
}
?>
I agree with the others on the usage of:
switch ($i) {
case 0: //drop
case 1: //drop
case 2: //drop
echo "i is 0, 1, or 2";
break;
// or you can line them up like this.
case 3: case 4: case 5:
echo "i is 3, 4 or 5";
break;
}
The only thing I would add is the comments for the multi-line drop through case statements, so that way you know it's not a bug when you (or someone else) looks at the code after it was initially written.