Stepping through countdowns, unsure how to do this - php

I have a somewhat working countdown script already with a defined time (using new DateTime) but haven't figured out how to automatically swap times daily.
I'm looking to do a countdown every day at 5:10, 5:50, and 6:15 PM UTC. If 5:10:00 passes, then swap the countdown for 5:50:00. If it's 6:16, then it'll show for 5:10 tomorrow
How would I be doing comparisons for this? It says something about a non-object when I try to just use the datetime'd variable in a > <

Rather than comparing the objects themselves, you can compare their timestamps. For example:
if($dateTime1->getTimestamp() > $dateTime2->getTimestamp()) {
// ...
}

Related

I want to check if time is between a period with PHP for Financial Markets

I need to check which financial markets are open but i didn't found api for it.
I want to make it with php, and i need to give for example 2 periods of time:
$open = 09:00:00;
$close = 16:00:00;
and add a if statement as a checker if time is between those, and i will run a cron job for it.
All i want is to get the correct condition to check if time is on this period/frame.
Tried this but i dont know how to implement further:
$datetime1 = new DateTime('03:55:06');//start time
$datetime2 = new DateTime('11:55:06');//end time
$interval = $datetime1->diff($datetime2);
echo $interval->format('%H hours %i minutes %s seconds');//
First of all, you should consider using the same zoned datetime for the financial market you are targeting.
For Paris as an example, you will use "Europe/Paris" to get the local datetime inside that timezone.
After that point, you can go further with your between condition. And the best way to get that is to use the DateInterval class, with its own DateInterval::diff method.
You can check out the official PHP documentation here : https://www.php.net/manual/fr/class.dateinterval.php
And the diff method : https://www.php.net/manual/fr/datetime.diff.php
And I would compare the current zoned datetime to the start of the working interval : it must be equal or later.
I would compare it to the end of the working hours interval : it must be before or equal.
So you have everything you need to write it correctly now.

PHP check if current time is between two times

I have a start and end time in milliseconds.
I have to get all the TV series that are ON AIR when the user visits the page.
So I am trying to do this:
if($prog["inizio"] < time() && $prog["fine"] > time()){
array_push($programmazioneFinal[$date."-".$prog["id_canale"]], $prog);
}
The logic is to get only those series whose starting time is lower than now (the serie is already started) and the end time is bigger than now.
For some reasons it is also returning those series that start much later in the day, not just the ones ON AIR now.
What's wrong?
I have added a screenshot of my DB just to make this clearer.
Thank you!
In PHP: You can use strtotime. This will give you timestamp value which you can use for comparison
strtotime("now")
Edit in your code
if($prog["inizio"] < strtotime("now") && $prog["fine"] > strtotime("now")){
array_push($programmazioneFinal[$date."-".$prog["id_canale"]], $prog);
}
Make your query look like
Select * from ... WHERE UNIX_TIME($your_date_parameter) BETWEEN inizio AND fine

"Half Hour" Intervals for date Method

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.

Comparing two date / times to find out if 5 mins has lapsed between the two times php

I need to compare two dates to show an edit link if it is within 5 mins after the post was made, in PHP. If more than 5 minutes have passed, don't show anything.
$answer_post_date = get_the_time("Y-m-d");
$current_date = date("Y-m-d");
$formated_current_date = strtotime($answer_post_date);
$formated_answer_post_date = strtotime($current_date);
At this point I have two values:
1274414400 ($formated_current_date)
1276056000 ($formated_answer_post_date)
I am not sure what to do next to check if the current date/time is > 5 mins from the answer post date.
Any suggestions would be great.
All I really need the answer to be is a Boolean (yes/no) and if yes, display the minuets left to show the link to edit.
You're only handling dates, how are you supposed to know if the difference is 5 minutes?
Anyway, I'd say the majority of the PHP code that uses the default PHP functions is at least somewhat broken. The problem is you, despite a unix timestamp storing the correct point in time something happens, it does not store timezone information. See here.
So, forget using only date and strtotime. Use the datetime extension.
Store in the database the Unix timestamp and the timezone (by timezone I mean e.g. Europe/Lisbon). Then:
$tz = new DateTimeZone($timezone);
$answer_post_date = new DateTime("$timestamp");
$answer_post_date->setTimeZone($tz);
$current_date = new DateTime("now", $tz);
$diff = $current_date->diff($answer_post_date);
if ($diff->format("a") > 0 ||
$diff->format("h") > 0 ||
$diff->format("m") >= 5) {
//more than 5 minutes have passed
}
Of course, for comparing dates, you can always compare the timestamps.
My understanding of what you need to do:
$delta = ($formated_current_date - $formated_answer_post_date) / 60; // in minutes
if ($delta < 5) {
// show $delta
}
EDIT: Like others pointed out, this alone will not fix all of the issues at hand. As I see it, the smallest change to your current code would be to use a date format with higher granularity - such as "Y-m-d H:i:s". This being enough, like others pointed out, is contingent on the post's date being in the same timezone as your system.
I don't see the need to do a round-trip to a string format and back, regardless of how efficient or reliable it is.
date() will default to calling time() which you can call directly and get the current time in seconds as a Unix epoch timestamp (which is what you're trying to end up with in $formated_answer_post_date). You need to look in the WordPress docs to find the equivalent based on the post's value.
Then you can do a simple comparison of seconds. 5 minutes is 300 seconds.
You will still need to check that the code can assume the timezones of both values will be the same.

Make strtotime() generate times only in the future

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

Categories