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.
Related
I am trying to show events that occur either today or on a later date where today is specifically the problem.
public function getInspirationsMeetingIds()
{
$ids = [];
if (($inspirationMeetings = $this->getCustomField('meetings'))) {
foreach ($inspirationMeetings as $meeting) {
$row = new Inspiration($meeting['meeting']);
$dateFrom = $row->getCustomField('date');
if (strtotime($dateFrom) >= time()) {
$ids[] = $row->getId();
}
}
}
return $ids;
}
For some reason this will only show events that are greater than time() and not the events that are today, but then when i try this:
if (strtotime($dateFrom) <= time()) {
$ids[] = $row->getId();
}
Today's and older events are shown.
I think you need to add a timestamp to your datefrom.
Strtotime will add noon if time is omitted.
See this example https://3v4l.org/cYKO4
if (strtotime($dateFrom ) >= strtotime(date("Y-m-d 00:00"))) {
Will make it show all of datefrom
Edit added the 00:00 at the wrong side
Use the DateTime class http://php.net/manual/en/class.datetime.php
time() gives seconds since Jan 1st 1970. The chance that you hit the exact second is very small, so it will hardly ever match.
Instead, create a date with the time.
$date = new DateTime($dateFrom); // or DateTime::createFromFormat($format, $dateFrom);
$today = new DateTime();
if ($date >= $today) {
// should work
}
I have a piece of PHP program that would supposedly identify the Shift time of a user when they login.. see below actual script condition.
$dt = new DateTime(date('H:i:s'));
$dt->setTimezone( new DateTimeZone("Asia/Manila") );
$timeNow = $dt->format('H:i:s');
if (strtotime($timeNow) >= mktime(07,0,0) && strtotime($timeNow) < mktime(15,0,0)){
$shift = "2nd [0700H - 1500H]";
}elseif(strtotime($timeNow) >= mktime(15,0,0) && strtotime($timeNow) < mktime(23,0,0)){
$shift = "3rd [1500H - 2300H]";
}else{
$shift = "1st [2300H - 0700H]";
}
The script above is working but there are records that falls into wrong shift.. for example a user access the page in 7:10AM which means should be in 2nd Shift but instead.. it falls into 1st Shift
I don't know what have I missed in this control flow.. so if anyone from here can help and share ideas that would be very much appreciated. Thank you!
There's a difference between using DateTime::setTimezone() and applying a timezone in the DateTime constructor. The former, converts the existing time to the DateTimezone specified as the argument to DateTime::setTimezone(), and the latter, assumes that the supplied formatted date is already in that timezone.
You're saying you want to know whether right now it's between 7 am and 3 pm in Manila, or between 3 pm and 11 pm, etc... So the idea is to compare only within that timezone. There is no need to do any timezone conversion here whatsoever.
$now = new DateTime("now", new DateTimeZone("Asia/Manila"));
$shift2 = new DateTime("7:00 AM", new DateTimeZone("Asia/Manila"));
$shift3 = new DateTime("3:00 PM", new DateTimeZone("Asia/Manila"));
$shift1 = new DateTime("11:00 PM", new DateTimeZone("Asia/Manila"));
if ($now >= $shift2 && $now < $shift3) {
// It's shift 2
} elseif ($now >= $shift3 && $now < $shift1) {
// It's shift 3
} else {
// It's shift 1
}
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.
Any idea in PHP how to get a unix timestamp of the next occurring specified hour and minute?
Thank you
Calling strtotime("4pm") will give you the time for today at 4pm. If it's already past 4pm, you can just add 60*60*24 to the given timestamp
// Example blatantly copied from AndrewR, but it uses strtotime
$nextfourpm = strtotime("4pm");
if ($nextfourpm < time()) {
$nextfourpm += 60*60*24;
}
You could do something like this.
$nextTime = mktime(16, 0, 0);
if($nextTime < time()){
$nextTime += 86400;
}
Now is 15:20
You want tomorrow at 16:22
BEST way for you is to create function with posting hour+ and minutes+ you want to go beyond - pure difference in time). By doing this way PHP cannot make any mistakes. And PHP can make mistakes while working with dates and stuff.
//stuff to post..
$hour = 25;
$minute = 2;
function makemydate($hour,$minute)
{
if($hour > 0)
{
$hour = ($hour*3600);
}
if($minute > 0)
{
$minute = ($minute*60);
}
$add = ($hour+$minute);
$now = time();
$unixtimestamp = ($now+$add);
return $unixtimestamp;
}
echo makemydate;
There you go its a unix timestamp.. You need to specify time whatsoever :P
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).