Php time if statement

How do i constantly check a if statement in php?

I have a php if statement to check server time, if is certain hour it runs a jquery function. problem is it only gets fired once when page loads. i know how to it with javascript, but i need the if statement to be php cause i need to check the server time, not current time of user browser. something like check it every 10sec would be ideal i guess the code in the footer.php:

= 8 && $hour   else < ?>  ?> 

I don’t even know if you are asking can be done in php (I’m not so expert in it), but I suggest to do it in JS, anyway. You say you need server time: you cannot check it from javascript, but you can calculate at load time the difference between server time and user time, and use this value to correct user time and make javascript run your jquery exactly when you need it.

1 Answer 1

Your PHP code is executed once when the page is loaded. Only when PHP is finished does the resulting HTML and Javascript get sent to the browser so your visitors have something to see.

If you want to periodically check something while a user is on your website, that’s a job for Javascript and not PHP. Since you need to check the server time instead of the user’s local time, your best bet is to use Ajax to call a PHP script every so often.

Читайте также:  Screen size height css

Judging by what you want to do, all you really need to know is what hour the server thinks it is. I personally prefer these kinds of PHP scripts to output JSON, mainly because Javascript can deal with it so easily, so a simple PHP script like this would be enough:

This will output a JSON object that contains the current hour according to the server, for example:

You can use that relatively simply with jQuery. Wrap it in a setInterval to have it run periodically. You mentioned every 10 seconds, so that’s what I’ll use for this example:

  

Quick disclaimer: I didn’t test this code, so YMMV. It looks like it should work, but I didn’t intend to fully solve your problem for you, rather to point out the way to go about it.

Источник

in php i need one line if condition for time compare

Oh come on. How do you want to learn the language this way? See the manual: de2.php.net/manual/en/control-structures.if.php Why not try to build something, and put it up here for scrutiny / optimization.

no sir?? actuallay i m getting confused on each level although i have one year experience on PHP. but i really facing this kind of problem, so where do i ask? sorry for asking so basic question, but i m not getting the required result then i post here.

You will always get an answer on SO, but you really need to learn this for yourself. Otherwise, it’ll take another year to get the control structures. Check out the manual I link to, there are plenty of examples there.

Sometimes examples that solve real problems are a good way to see logical concepts illustrated. I think it’s highly unfair to penalize the Asker so drastically for requesting help in this case. Those of us who are experienced see this as a simple problem, but it can take time to train the brain to think this way. Quit being jerks.

4 Answers 4

Keep in mind that 0, null, and blank all mean completely different things here. As indicated previously, strtotime will never return NULL. However, 0 is a valid unix timestamp, whereas false means that the strtotime function was unable to process the value provided.

Also, you’ve requested that a single-line solution; however, in my opinion, it is much better in this case to write out each condition and display a different error message for each condition. That way, the user knows what actually went wrong. Perhaps this is a better way:

// Only check for errors if we have at least one value set if (!empty($input['MondayOpen']) || !empty($input['MondayClosed']) < $mo = strtotime($input['MondayOpen']); $mc = strtotime($input['MondayClosed']); $invalid = false; if (false === $mo) < echo "Invalid Opening Time\n"; $invalid = true; >if (false === $mc) < echo "Invalid Closing Time\n"; $invalid = true; >if (!$invalid && $mc if ($invalid) < exit(); // Or handle errors more gracefully >> // Do something useful 

very expressive answer. thank you sir. i was really scared when so many people behave so rudely when i post so basic question.. actaully i was puzzled after converting into strtotime ans then comparing

Also, although 0 is a valid unix timestamp and it’s important to illustrate the weakly typed pitfall, is it really necessary to check for it here?

@Mike Sherov yes sir it is necessary to check. is there any other method to check.. bcoz after this validation if everything is fine then i apply date() on $mo and $mt , and if there is 0 in field then date() convert it to some value. that’s y i need to check these all conditions

It checks whether $mo and $mc are valid dates using is_numeric . Any NULL or false values will be caught by that. I haven’t tested it but it should work.

I spread it into a huge block of code. In the beginning, when learning the language, this is the best way to make sense out of the code. It is not the most elegant, nor by far the shortest solution. Later, you can shorten it by removing whitespace, or by introducing or and stuff.

I’m not 100% sure about the number comparison part, and I don’t have the time to check it right now. You’ll have to try out whether it works.

You need to decide how you want to handle errors and insert the code to where my comments are. A simple echo might already do.

// If $mo or $mc are false, show error. // Else, proceed to checking whether $mo is larger // than $mc. if ((!is_numeric($mo)) and (is_numeric($mc))) < // Error: $mo is either NULL, or false, or something else, but not a number. // While $mc IS a number. >elseif ((!is_numeric($mc)) and (is_numeric($mo))) < // Error: $mc is either NULL, or false, or something else, but not a number. // While $mo IS a number. >else < if (($mc else < // Success!! >> 

Источник

if statements and time php [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.

The user enters a time to hire a bus and the length of hours. I then query this information from mysql database. I find the end time of the hire and figure out how many hours after midnight the hire is for, if any. I’m pretty sure I had this working but now it just isn’t working and I cannot see why it won’t. Finding the end time is right but my if statement doesn’t seem to work. For example, when the end time is «02:00:00» so 2am, it follows the path of the if statement so that the time after midnight is 3 hours, when it should be 2 hours. Any help as to why? Thanks. ($hireid = latest hireid in database)

//GET LATEST TIME AND LENGTH OF HIRE $result = mysql_query("SELECT time, length FROM hire WHERE hireid='$hireid'") or die ('Error: '.mysql_error ()); while ($row = mysql_fetch_assoc($result)) < $time = $row['time']; $length = $row['length']; >//FIND AMOUNT OF HOURS BEFORE AND AFTER MIDNIGHT echo "Time: " . $time . "
"; echo "Length: " . $length . "
"; echo "Hours: " . $hours . "
"; echo "End time: " . $endtime = date('H:i:s', strtotime($time . '+ ' . $length . 'hours')) . "
"; $hoursafter = 0; if ($endtime = "01:00:00") < $hoursafter = 1; >if ($endtime = "02:00:00") < $hoursafter = 2; >if ($endtime = "03:00:00") < $hoursafter = 3; >echo "Hours after midnight: " . $hoursafter . "
";

Источник

PHP if Time or Date Greater than Show Echo

how can $vsp be greater than 10:00:01 if that’s what it’s set to? Maybe I’ve understood it wrongly though.

5 Answers 5

If you wanna work with real time use this but I will much more consider working with UNIX_TIMESTAMP :

$Date = date('H', strtotime('10:00:01')); if($Date > 10) < echo 'Well, 11, 12, 13, 14 . till 23'; >else

Yes, create a new variable and test it with $Date > 10 OR $NewVar < 16 .. I don't know what you wanna do.

Use strtotime() function to convert string to time.

My own solution to this, to avoid GMT response and server drain. Use JavaScript universal) v8 engine.

My code IS working now, the best solution for this fix is to use 16, 16.00, 04.00, 4.00 so it covers ALL combinations of 4 o clock!

If you have like this, then you can use :

if ( $mydate > strtotime('2013-03-19 17:14:40') )

This question is in a collective: a subcommunity defined by tags with relevant content and experts.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.27.43548

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Оцените статью