how to check depending on hour base - php

I have a field that has time and date in following format
2010-03-26 10:06:11
What I need is that if this time is within 4 hours of current time then show but if its over 4 hour then done show this record.
thanks

$ts = strtotime($value);
$curtime = time();
if (($ts > $curtime - 4*3600) && ($ts < $curtime + 4*3600)) {
//show
}
else
//don't show
You can also make a one-side comparison by choosing only one of the conditions (it isn't clear what you want from the question).

Related

hide message only during the set time

I am trying to hide a message between 2 particular timings and rest of the timings it should display.
the timings are
07:30am to 10:30am
and
18:30pm to 00:00am midnight
currently i did the following code which works ok for 07:30am to 10:30am it does the job but now how do i combine the 18:30pm to 00:00am midnight??? please someone help me out with some good logic.
<?php
date_default_timezone_set('America/New_York');
$now = new DateTime();
$fromTime = new DateTime();
$fromTime->setTime(07,30);
$toTime = new DateTime();
$toTime->setTime(10,30);
$fromTime1 = new DateTime();
$fromTime1->setTime(18,30);
$toTime1 = new DateTime();
$toTime1->setTime(00,00);
if ( $now < $fromTime || $now > $toTime) {
?>
This is a test message
<?php
}
?>
I tried doing like this
$now < $fromTime || $now > $toTime && $now < $fromTime1 || $now > $toTime1
but it messes up and displays the message even during 07:30am.
Thanks for your help
It's prety simple, to check few dates interval, but, don't make mistakes with PHP logical operators (https://www.php.net/manual/en/language.operators.logical.php) :
if ( ($now >= $fromTime && $now <= $toTime) // If it's on the first interval
|| // Or
($now >= $fromTime1 && $now <= $toTime1) // If it's on the second one
) {
echo 'Appear on the interval'; // I display the message.
}
Also, you've a mistake with your last variable $toTime1, you must to check from 18::00 to 00:00 ... Not from the current day ! (but 00:00 to the next day).
You can edit your code like this, by addind +1 day to your last variable :
$toTime1 = new DateTime();
$toTime1->modify('+1 day');
$toTime1->setTime(00,00);
Like this, your timings are good ;)
Don't forget this code (php) is managed by the server side. So, if your user don't reload the page, your message will be visible during your intervals. Maybe you can use JavaScript for hide the message during your intervals on the client side, in the case that the user remains on the page ;)
Hope I help you.

How can I substract 15 min from a start-time?

I tried to substract 15 min. from another time (start-time).
I want to check if the current time is 15 min. befor a meeting starts.
foreach($result->value as & $value) {
$start = $value->Start->DateTime;
$startmeeting = substr($start, 11, -11); //cut time to hour:minute
$now= date('H:i', time());
$min= strtotime('-15 minutes');
$timebefor = date($startmeeting, $min); //Here I want to substract starttime with 15 min
if( $now >= $timebefor && $now <= $startmeeting )
{
//Show yellow warning box
}
}
Is it even possible on this way?
You basically have your solution, but it is untidy, and contains bugs. I think you want to do something like this:
foreach ($result->value as $value) {
$meetingStart = strtotime($value->Start->DateTime);
if (($meetingStart > time()) &&
($meetingStart < strtotime('15 minutes')))
{
//Show yellow warning box
}
}
Simply put: If the meeting is in the future, but less than 15 minutes into the future, you will have to show the yellow warning box.
When programming always pay attention to the names you choose. Notice how I use $nowPlus15Minutes which does clearly indicate what that variable contains. You used $min which isn't very self explanatory. The same problem exists with names like $value and $start. Perhaps $timebefor is a misspelling?
I recommend that you use PHP:DateTime with this. If your system works with external API (such as Google Calendar), I will usually specify the timezone too.
$currentTime = new DateTime("now", new DateTimeZone("Asia/Singapore"));
$reminderTime = new DateTime("2019-08-08T12:00:00.0000000", new DateTimeZone("Asia/Singapore"));
$reminderTime->sub(new DateInterval("PT15M")); // PT means period time, 15 minutes.
// Comparison of DateTime is allowed from PHP 5.2.2 onwards
if($currentTime > $reminderTime) {
// Do something
}
// For DEBUGGING
echo $currentTime->format('Y-m-d H:i:s') . "\n" . $reminderTime->format('Y-m-d
H:i:s');
Refer to DateTime documentation for more information.

Finding the difference between 2 dates in PHP

I am using PHP, jQuery AJAX and HTML to create a timesheet system, for this the user needs to select 2 dates within 1 month of each other. The system as yet is working and shows (very limited) data.
BUT! When I actually select a date over the month limit (i.e. 2 months further than the start or another year after the start), it still shows the table with the data.
For this I have this check:
$dt1 = new DateTime($_REQUEST['startdate']);
$dt2 = new DateTime($_REQUEST['enddate']);
$diff = date_diff($dt1, $dt2);
// I have tried this the other way around and get the same result...
if($diff->m > 1 || $diff->y > 1)
{
print("<center><strong>Time between dates it too great<br />Please choose another date or time within a month of each other</strong></center>");
die();
}
The dates are passed by a jQuery datepicker object via AJAX, and the dates I use, for example, are passed as such:
11/14/2015 (start date) && 12/14/2015 (end date) - should show data
09/14/2015 (start date) && 12/14/2015 (end date) - should not show data but does
11/14/2015 (start date) && 12/14/2016 (end date) - should not show data but does
There is a check in place that sees if the dates given start before the other and this works, I have tried the same kind of thing for this check, but without success, this check is as such:
function CountDaysBetween($startDate, $endDate)
{
$begin = strtotime($startDate);
$end = strtotime($endDate);
if ($begin > $end) {
echo "start date is in the future! <br />";
return;
} else {
$no_days = 0;
$weekends = 0;
while ($begin <= $end) {
$no_days++; // no of days in the given interval
$what_day = date("N", $begin);
if ($what_day > 5) { // 6 and 7 are weekend days
$weekends++;
};
$begin += 86400; // +1 day
};
$working_days = $no_days - $weekends;
return $working_days + 1;
}
}
Edit
Dates 2 or more months apart within the same year work, tested again and this is the case, but dates into the next year do not
In your first part of the php code, you have put this operator>, but the problem is it means, everything Smaller than 1, not everything that is smaller than one or equal to 1. The easy solution is to change the operators to >=; which means everything that is equal to 1 or smaller than 1.
The date_diff constructs in PHP suck monkeyballs. Far more practical is to use straight comparisons instead:
$dt1 = new \DateTime($_REQUEST['startdate']);
$dt2 = new \DateTime($_REQUEST['enddate']);
$dt1->add(new \DateInterval('P1M'));
echo ($dt1 < $dt2 ? 'Less' : 'More') . ' than a month';
Also please do not use $_REQUEST, it has potentially terrible security issues. You should use $_GET, $_POST or $_COOKIE according to what you explicitly expect.

(PHP) Display link ONLY during business hours?

I am trying to make a live chat link appear on the website only during business hours. I have the code below which seems to work in the afternoon, but won't work in the morning and I'm not sure why... $start and $end are values received from a MySQL database but in my example I've hard coded them to make the example simpler.
$LinkStatus = "on";
$start = 9:00:00;
$end = 23:00:00;
$current_time = date('G:i:s'); //9:35:00
if (($start > $current_time) || ($end < $current_time)) {
$LinkStatus = "off";
}
If the start time is greater than the current time, then the business is not open yet. If the end time is less than the current time, then it's after hours. Any time between 9am and 11pm (23:00) neither one of those conditions should be true, therefore $LinkStatus should remain "on". However, it does not seem to be doing that right now. Something is setting it to "off".
I've echoed the variable above the if statement and below it so I can confirm it's this if statement causing the variable to be set to "off".
As you can probably see from my code example, I'm not very knowledgeable when it comes to PHP. Any help is appreciated.
date('G:i:s') // 24 hours time without leading zero for hour
...won't sort well as a string, for example '9' > '10'.
Use 24 hour time with a leading zero instead, which makes the correct sort '09' < '10';
date('H:i:s') // 24 hour time with leading zero for hour
date_default_timezone_set('Europe/London');
$day_start = '09:00:00';
$day_end = '16:59:59';
$current_time = date('H:i:s'); // For 9-5 hours only
$current_day = date('N'); // For Monday to Friday only
if (($current_day <= 5) && ($current_time >= $day_start) && ($current_time <= $day_end)) {
echo 'ON';
} else {
echo 'OFF'
}

Show content between two dates with PHP?

I want to be able to show some content or echo some text between and on two dates.
Its for a promotion of a product, between the dates given the price will be lower and after the promotion the price will return to the original cost.
Is this possible?
Cheers
Matt
$current_time = time();
if($current_time > $start_time && $current_time < $end_time){
// Whatever you want during the time interval
}else{
// Whatever you want outside the time interval
}

Categories