This seems like a basic question and I don't see if it has been asked before:
I have this if statement in a script:
if (date('H') < $loc_item -> closing) {
Basically it's a script for business. And it has to do with when a store closes. 6 o'clock, 7 o'clock, etc.
The variable uses values 1 - 24 for hour only. However, SOME business close at, 5:30 PM (17:30), 6:30 PM (18:30),
Would a value of 18.5 represent 6:30 PM? If not, what is the simplest way to enter use the date function where I can add a value of 1830 and it knows I mean 6:30PM?
Edit: There is NO user output here. The script just needs to know to throw a "switch" at a certain time of day.
You could use strtotime()
date("Hi", strtotime("18:40"));
If you want the date function to return hours and minutes, then date('H') isn't going to do it for you. You need date('Hi'). That returns a string. The following is a complete code snippet:
<?php
date_default_timezone_set('America/New_York');
echo "The time is ".date('Hi')."\n";
$closingTime = "12:15";
echo "The store closes at ".$closingTime."\n";
if (strtotime(date('Hi')) < strtotime($closingTime)) echo "it is still open\n";
else echo "the store is closed\n";
?>
Sample output:
The time is 1225
The store closes at 12:15
the store is closed
You aren't really giving us enough information to answer the question.
First off, you might as well make your script support all minutes...cause some stores might close # 6:45.
I think the solution you are looking for is to simply do a comparision of timestamps.
The easiest way to do this is using strtotime.
I would tend to do something like:
$now = new DateTime();
$closing = new DateTime('19:30');
if ($now < $closing) // not closed
I assume you don't have to worry about stores closing in the early AM.
Related
This question already has answers here:
How to check if time is between two times in PHP [duplicate]
(2 answers)
Closed 8 years ago.
I'm sure this is a pretty basic task, but I'm quite new to programming and am a bit confused.
Basically I want to execute a different action based on the time of day. For example, if the time is between 05:00 and 20:00 (8pm), run "scriptA". ELSE, or if time is between 20:00 and 05:00, run "scriptB".
What is the easiest way to do this? I basically want to run one script during the "Day" and the other at "Night".
Thank you!
You can try using the date function.
echo date("H:i:s");
will display the current time. Get this string. Compare it and use an if/else.
PHP Date Reference Manual
<?php
if((date("H") > 5) && (date("H")<20) )
{
//run_my_day_script
}
else
{
//run_my_evening_script
}
?>
Get the hour of the day in a 24 hour format, without leading zeros.
$hour = (int)date("G");
Then you can do a less or greater that check.
if ($hour >= 5 && $hour <= 20)
{
// Do Something.
}
http://php.net/manual/en/function.date.php
I have a list of timestamps that represent a list of backed up files. But to reduce the amount of space needed I only want to keep the files that are from around mid day- I have started writing a function check but got stuck on how could i check if the timestamp is between 12 and 1 for that day? I have a list of timestamps for many days.
function check_date($timestamp='')
{
if (($timestamp < strtotime("-1 week")) && (time is between 12 and 1 )){
}
else
remove
}
I wrote an answer to this previously, I will try find a link in a minute when im free but essentially get the timestamp for 12 and the timestamp for 1 then
if(timestamp12 < curTimestamp && curTimestamp < timeStamp1)
Then you know that the curTimeStamp is between 12 and 1.
Previous answer https://stackoverflow.com/a/11578345/1475461
The other question was for a javascript implentation but timestamp comparison works the same whether you are using PHP, javascript etc. since they are miliseconds/seconds since a set point in time (1st January 1970), so it is just a comparison of integers.
Okay, let me think...ah, here it is. Good old 'localtime' function.
http://www.php.net/manual/de/function.localtime.php
Run your timestamp through this function, then you can check the result.
$TimeInfo = localtime(timestamp, true);
if (($timestamp < strtotime("-1 week")) && $TimeInfo["tm_hour"] == 12) {
}
else remove
With this code, all files, which have arrived at 12:00, up to 12:59 (1 is excluded in this example) are preserved, the others get removed.
It's a simple question..but drive me 2 madness.
the result of this simple line of code:
echo gmdate('Y/m/d H:i:s');
... must output GMT time but it get it minus 1 hour!!!!!
So why??
Greenwich Mean Time has no "Summer Time" or "Daylight Saving Time" so depending on the season of the year these statements may produce the same or different output.
date_default_timezone_set('Europe/London');
echo gmdate('c');
echo date('c');
-- from the PHP manual (so, in addtion to the answer you get a clear RTM ;)
More information about timezones and daylight saving on SO.
Compare the output of your script with:
http://www.worldtimeserver.com/current_time_in_UTC.aspx
If it's wrong, the computer where you run the code is probably misconfigured.
Hi guys I was wondering if anyone could help me with the following:
I have two dates entered in two different fields > startDate and endDate.
As they are entered I would like to show a warning if:
the second one is a date before the first one. So it is wrong.
and that between the first one and the second one there a minimum gap of at least 3 days during certain period of the year and 7 days during other periods of the year.
I was thinking to write a PHP function but how do I call it as soon as the second date is entered?
Many many thank for you help
Francesco
Convert your dates to Julian day with gregoriantojd.
/**
* Get the Julian day of a date. The Julian day is the number of days since
* January 1, 4713 BC.
*/
function datetojd($date)
{
return gregoriantojd(idate('m', $date),
idate('d', $date),
idate('Y', $date));
}
// you can use strtotime to parse a lot of date formats, assuming they are text
$startDate = strtotime('22nd Nov 2009');
$finishDate = strtotime('26nd Nov 2009');
$diff = datetojd($finishDate) - datetojd($startDate);
if ($diff < 0) {
// oops, $finishDate is before $startDate
}
else {
// check $diff is at least 3 or 7 depending on the dates
}
Do the check on the client side with Javascript.
Then perform the same checks server side which can present a message after the form has been submitted (for those few users running with Javascript disabled?).
I'm not sure if you can call it as soon as the second date is entered, unless you reload the page or have the function on another page which could get a tad complicated
The way i would check the dates is to use php's mktime function, which will give you the unix time. Then if the second one is less that the first, the second date is before and if the second one is less that the first + 3 * 24 *60 * 60 (seconds in 3 days) then it isn't 3 days apart
1° case:
SELECT [whatever you need from the table] WHERE endDate < startDate
2°case:
SELECT [whatever you need from the table] WHERE (endDate - startDate) >= IF([select that define in wich period of the year the data are],3, 7)
This ill do the trick, but probably your problem cant be solved sql-side.
Please, describe better what you need to do.
EDIT:
Ok, then as someone else suggested, first check htem by js (for convenience, not for safely: never rely only on js validation!)
Use strtotime for the comparison/operation.
EDIT2 (last;) :
Go with Alex's Answer
I'm trying to make a PHP script to find the next occurence of a date (say 5-1). However, if I put that into strtotime() on 5-2 it comes up with the date from this year. I simply want to make it always return the next date possible. How can I make this work? I'm using codeigniter if that helps.
EDIT: Here's some code I came up with, if some humble soul runs across the same problem:
if (strtotime($date) < strtotime("now")) {
$realseconds = strtotime($date) + 31556926; //add one year in seconds
} else {
$realseconds = strtotime($date);
}
You could check whether the date returned is earlier than the current time, and, if it is, add one year to it.
You could also pass some date in the next year as the second parameter to strtotime.
What is 5-1 or 5-2?!
Try doing this instead:
strtotime('next October');
Assuming 5-2 means february fifth, you could do
strtotime("february 5 +1 year")
Code:
$x=strtotime($inputString);
$YEAR=60*60*24*30*12;
while($x<$time()) $x+=$YEAR;
Basically, it adds one year if the date returned by strtotime is in the past... because i used while() it will never return a date in tha past even if it was explicitly stated like that