I'm seem to be having a problem with my if condition.
Whenever I enter an license that is expired more than 31 days it goes inside the condition where when then license expiry date is greater 31 days it will give a green coloured circle and green text.
It reads it as both expired and will not expire until 31 days giving a red and green coloured circle and text.
Here's a snippet.
$exp = strtotime($exp_date);
$td = strtotime($today_date);
$diff= $td -$exp;
$x = abs(floor($diff/ (60 * 60 * 24)));
$alertdays = ""; //expired
$alertdays1 = ""; //less than 30 days before expiry
$alertdays2 = ""; //more than 30 days
$statusexp = ""; //expired
$status = ""; //less than 30 days
$status1 = ""; //more than 30 days
$daysexp = ""; //days expired
if($td > $exp)
{
$statusexp = '<i class="fa fa-circle" style="font-size:30px; color:red;"></i>';
}
if($diff < 0){
$status = '<i class="fa fa-circle" style="font-size:30px; color:orange;"></i>';
}
if($diff <= 30) {
$alertdays1 = $x. "";
}
if($x >= 31){
$alertdays1 = "";
$alertdays2 = $x. "";
$status ='<i class="fa fa-circle" style="font-size:30px; color:green;"></i>';
}
if($td > $exp){
$alertdays ="Expired";
$daysexp = " - " . $x;
}
Output
The problem of your code is that you use ABS function to calculate the difference of today and the target date. If you use 2018-02-01 to test it, the result should be -41, but with ABS you got 41, so you code give "green" result.
you can create function
for example:
<?php
function Mehrdad_Diff_Alldate($startTime) {
$start_date = new DateTime($startTime);
$endTime = date("Y-m-d H:i:s");
$since_start = $start_date->diff(new DateTime($endTime));
$text=$since_start->y.'|'.$since_start->m.'|'.$since_start->days.'|'.$since_start->h.'|'.$since_start->i.'|'.$since_start->s.'|';
return $text;
}
$td = date("Y-m-d H:i:s");
$exp="2007-08-12 13:10:56";
$exps = Mehrdad_Diff_Alldate($exp);
$explode=explode("|",$exps);
$year=$explode[0];
$month=$explode[1];
$day=$explode[2];
$hours=$explode[3];
$mintue=$explode[4];
$second=$explode[5];
?>
Now you can use from $day
Just try to not divide these terms separately
for example:
<?php
if($td > $exp){
}else if($day < 0){
}else if($day <= 30) {
}else if($day >= 31){
}else if($td > $exp){
}else{
}
?>
Related
I have a list of baby logs, and I've tried to auto calculate the time difference between the previous ones, subtract the feeding interval.
feedingInterval = 3h
Ex.
If I fed my baby at 5PM, and if I fed him again at 7:50 PM.
He's early 10 mins.
More than 3h, it's green(+) means the baby sleep well
Less than 3h, it's red(-) means the baby will feed early, and we shouldn't do too often.
I have this block of code
if($baby){
$dateTime = new \DateTime();
$date = $dateTime->format('Y-m-d');
if(Request::get('date') != null) {
$date = Request::get('date');
}
$yesterday = date('Y-m-d',strtotime("-1 days"));
$logFromYesterday = BabyLog::where('type', $type)->whereDate('created_at', $yesterday )->orderBy('created_at', 'desc')->where('babyId',$baby->id)->get()->first();
$babyLogs = BabyLog::where('type', $type)->whereDate('created_at', '=', $date)->orderBy('created_at', 'desc')->where('babyId',$baby->id)->get();
$logDetail = [];
$lastFeedStatus = 'n/a';
foreach ($babyLogs as $i => $feed) {
$id = $babyLogs[$i]->id;
$t1 = $babyLogs[$i]->updated_at;
if($i >= (count($babyLogs)-1)){
if($logFromYesterday){
$t2 = $logFromYesterday->updated_at;
} else {
$t2 = $date.' 00:00:00';
}
}else {
$t2 = $babyLogs[$i+1]->updated_at;
}
// dd($t1,$t2);//
$diffTime = abs(strtotime($t1) - strtotime($t2));
$diffTime = $diffTime / ( 60 * 60 );
if($type == 'feed'){
$diffTime = $diffTime - $baby->feedingInterval;
$diffTime = round($diffTime,2);
$diffTime = $diffTime*60;
$logDetailStatus = ($diffTime>0) ? "+" : "-";
$diffTime = $logDetailStatus.str_replace('-', '', $diffTime). "m ";
} else {
$diffTime = round($diffTime,2);
$diffTime = $diffTime*60;
$diffTime = str_replace('0', '',date('H\h', mktime(0,$diffTime))) . '';
}
$logDetail[$i]['id'] = $id;
$logDetail[$i]['msg'] = $diffTime;
if($i == 0){
$lastFeedStatus = $diffTime;
}
}
return $logDetail;
}
I want to round to minutes only.
Ex. 10.2 m --> 10 m
I tried
round($diffTime,2);
to
round($diffTime,1);
I got
Ex. 10.2 m --> 12 m
Try using number_format():
<?php
echo number_format("100.1234");
echo number_format("100.50");
echo number_format("100.890");
?>
Result will be:
100
101
101
I'm working on a calendar for my office. Every people has his own column and there is a line for every day.
There are some periodic date, where, for example, given people have to be working on the week-end. But most of the dates are coming from a MySQL database.
So it does a double loop (people vs dates) in which every dates have to be check for the person, what kind of occupation he has on this day.
Is there a way to optimize this script, because online it take at least 2-3 seconds (only 0.03 seconds according to PHP, but I don't feel like it's correct) and more than 8 seconds (again according to PHP) on our network! And this is just for 5 months, we'd like to have it for the whole year.
You can find a test version here (just to see the HTML and CSS): http://mybees.ch/for/tableau.php
And here is the PHP in it:
//Creating the line for the collaborators
$ids;
$ma_count=0;
$ma_name_query = mysqli_query($bdi,"SELECT DISTINCT m.id, m.name, m.vorname, m.grup FROM ma m, ma_pos mp WHERE m.id = mp.maid AND m.grup != 14 ORDER BY m.grup, mp.posid, m.name, m.vorname");
while($ma_name = mysqli_fetch_assoc($ma_name_query)) {
echo '<div class="cell name">'.$ma_name['name'].' '.$ma_name['vorname'].'</div>';
$ids[$ma_count] = $ma_name['id'];
$grup[$ma_count] = $ma_name['grup'];
$ma_count++;
}
// Check if special group day
$firstGroup = 1;
$firstDate = strtotime("$year-01-01 00:00 GMT");
$picket = array();
if (date("N", $firstDate) == 2)// Tuesday
$firstDate -= 24 * 3600;
elseif (date("N", $firstDate) == 3)// Wednesday
$firstDate -= 2 * 24 * 3600;
elseif (date("N", $firstDate) == 5)// Friday
$firstDate -= 24 * 3600;
elseif (date("N", $firstDate) == 6)// Saturday
$firstDate -= 2 * 24 * 3600;
elseif (date("N", $firstDate) == 7)// Sunday
$firstDate -= 3 * 24 * 3600;
for ($date = $firstDate; $date <= strtotime("$year-12-31 00:00 GMT"); $date += 24 * 3600) {
$weekNb = date("W",$date);
$weekDay = date("N",$date); // Monday = 1, Sunday = 7
if ($weekDay < 4)
$group = $weekNb % 4 - 1 + $firstGroup;
else
$group = $weekNb % 4 - 2 + $firstGroup;
if ($group == 0)
$group = 4;
if ($group == -1)
$group = 3;
$picket[$date] = $group;
}
$groupColor = ["yellow", "blue", "red", "green"];
function isPicket($date, $grup) {
global $picket, $groupColor;
//global $picket, $groupColor;
if ($grup < 5) {
if ($picket[$date] == $grup)
return " style='background-color: ".$groupColor[$picket[$date]-1]."'";
}
}
$today_stp = time() - time() % (24 * 3600) - 11 * 24 * 3600;
$frei_query_text = "SELECT id_ma, date1, date2, free.id as type, moment, remark, location FROM frei, free WHERE free.id = frei.type AND date1 BETWEEN CAST('$date1' AS DATE) AND CAST('$date2' AS DATE)";
$frei_query = mysqli_query($bdi, $frei_query_text);
$free = array();
while($frei = mysqli_fetch_assoc($frei_query)) {
$free[] = $frei;
}
// Filling the lines
for ($date = strtotime($date1); $date <= strtotime($date2); $date += 24 * 3600) {
$today = ($date == $today_stp) ? ' id="today"':'';
echo "<div class='clear'$today>";
$class = (date('N', $date) < 6) ? 'week' : 'weekend';
echo "<div class='date $class line'>".date("D, d.m.", $date)."</div>";
for ($i = 0; $i < $ma_count; $i++) {
echo "<div class='cell line $class'".isPicket($date,$grup[$i]).">
<div class='small-cell".isColor($ids[$i], $date, 0)."' id='divUp-".$ids[$i]."-$date'> </div>
<div class='small-cell".isColor($ids[$i], $date, 1)."' id='divDown-".$ids[$i]."-$date'> </div>
</div>";
}
echo '</div>';
}
function isColor($id_ma, $date, $moment) {
global $free;
for ($i = 0; $i < count($free); $i++) {
if ($id_ma == $free[$i]['id_ma']) {
if ($date >= strtotime($free[$i]['date1']) && $date <= strtotime($free[$i]['date2'])) {
if ($free[$i]['moment'] == $moment || $free[$i]['moment'] == 2) {
$type = $free[$i]['type'];
$style = "";
if ($type > 1 && $type < 5)
$style = " urlaub";
if ($type > 4 && $type < 8)
$style = " frei";
if ($type > 7 && $type < 11)
$style = " ferien";
if (($type > 22 && $type < 34) || ($type > 37 && $type < 48))
$style = " kurse";
if ($type > 10 && $type < 17)
$style = " krank";
return " $style' title='".$free[$i]['remark'];
}
}
}
}
}
Thank you very much for your help
This code is used to take values inputted from a form but this does not take a year entered as 0100 as 0100 but as 1915, this is then used with the JS seen in one of my other questions any help here would be very good, I think the issue is something to do where the year is taken but I just can't get this to work correctly. Is this a limitation of php?
<?php
$year = "";
$month = "";
$day = "";
if (isset($_GET['year']) && !empty($_GET['year'])) {
$year = $_GET['year'];
}
if (isset($_GET['month']) && !empty($_GET['month'])) {
$month = $_GET['month'];
$monthNumber = date('m', strtotime("$month 1 Y"));
}
if (isset($_GET['day']) && !empty($_GET['day'])) {
$day = $_GET['day'];
}
if ($year != "" && $monthNumber != "" && $day != "") {
$fullUrlDate = $year . "-" . $monthNumber . "-" . $day;
$urlDate = new DateTime(date($fullUrlDate));
$today = new DateTime(date("Y-m-d H:i:s"));
$interval = $urlDate->diff($today);
$gapYears = $interval->y;
$gapMonths = $interval->m;
$gapDays = $interval->d;
$gapDaysTotal = $interval->days;
$gapWeeksTotal = round($interval->days/7);
$gapHours = $interval->h;
$gapMinutes = $interval->i;
$gapSeconds = $interval->s;
if ($gapWeeksTotal == 1) {
$gapWeeksSuffix = "";
} else {
$gapWeeksSuffix = "s";
}
if ($gapDays == 1) {
$gapDaysSuffix = "";
} else {
$gapDaysSuffix = "s";
}
$ordinalSuffix = date("S", strtotime($fullUrlDate));
if (strtotime($fullUrlDate) < strtotime(date("Y-m-d H:i:s")) ) {
$dateInThePast = true;
} else {
$dateInThePast = false;
}
// Months gap
$monthsInterval = date_diff($urlDate, $today);
$monthsGap = $monthsInterval->m + ($monthsInterval->y * 12);
$gapMonthsSuffix = ($monthsGap == 1 ? "" : "s");
DateTime has no such limitation, but the date function you use to initialise it, does. You can use DateTime::setDate to set any year you want:
php > $a = new DateTime("2015-08-24");
php > echo $a->format(DateTime::ISO8601);
2015-08-24T00:00:00+0000
php > $a->setDate(90, 8, 24);
php > echo $a->format(DateTime::ISO8601);
0090-08-24T00:00:00+0000
php > $a->setDate(90090, 8, 24);
php > echo $a->format(DateTime::ISO8601);
90090-08-24T00:00:00+0000
I'm currenty looking into this tutorial to create a calendar. The only problem I have atm is that my months are in english instead of dutch. How can I change the output of 'july' to 'juli' ?
<?php
$vandaag = date("d"); // Current day
$maand = date("m"); // Current month
$jaar = date("Y"); // Current year
$dagen = cal_days_in_month(CAL_GREGORIAN,$maand,$jaar); // Days in current month
$vorigemaand = date("t", mktime(0,0,0,$maand-1,1,$jaar)); // Days in previous month
$begin = date("N", mktime(0,0,0,$maand,1,$jaar)); // Starting day of current month
$einde = date("N", mktime(0,0,0,$maand,$dagen,$jaar)); // Finishing day of current month
$vorigestart = $begin - 1; // Days of previous month in calander
$counter = 1;
$volgendeMaandCounter = 1;
if($begin > 5){ $rows = 6; }else {$rows = 5; }
for($i = 1; $i <= $rows; $i++){
echo '<tr class="week">';
for($x = 1; $x <= 7; $x++){
if(($counter - $begin) < 0){
$date = (($vorigemaand - $vorigestart) + $counter);
$class = 'class="blur"';
}else if(($counter - $begin) >= $dagen){
$date = ($volgendeMaandCounter);
$volgendeMaandCounter++;
$class = 'class="blur"';
}else {
$date = ($counter - $begin + 1);
if($vandaag == $counter - $begin + 1){
$class = 'class="today"';
}
}
echo '<td '.$class.'><a class="date">'. $date . '</a></td>';
$counter++;
$class = '';
}
echo '</tr>';
}
?>
Thanks in advance!
try with this code:
setlocale(LC_TIME, 'de_DE', 'deu_deu');
/* print test date string */
echo strftime("%A, %d. %B %Y");
How to convert military time like this?
07:03 --> 07:30
06:45 --> 07:00
07:36 --> 08:00
19:15 --> 19:30
18:35 --> 19:00
19:35 --> 20:00
Basically the thing is, if the time is in the interval of 30 mins, you will add up the remaining minutes to make it a whole time.
In my database, I have to compare time like this where:
function check_range($get_shift, $date, $time, $id, $mode) {
$sql = "SELECT * FROM tbl_employee WHERE id = '$id'";
$result = mysql_query($sql);
$row_check_range = mysql_fetch_assoc($result);
$getShift = $row_check_range['shift'];
$sql2 = "SELECT * FROM tbl_shift WHERE shift = '$getShift'";
$result2 = mysql_query($sql2);
$row_check_range2 = mysql_fetch_assoc($result2);
$getTimeShift = $row_check_range2['timeIn'];
$actual_time_in_mode = explode(':', $time); //split time into (e.g 07, 11)
if ($mode == 'AM') {
$time_in_actual = $actual_time_in_mode[0];
} else {
$time_in_actual = $actual_time_in_mode[0];
$time_shift = explode(':', $getTimeShift);
$getTimeShift = $time_shift[0] + 12; //convert to military time
}
$getTimeShift = mktime($getTimeShift);
$actual_in_mode = mktime($time);
if ($actual_in_mode > $getTimeShift) // LATE
{
return $date.' '.$time_in_actual.':30:00';
} else {
$time_in_actual += 1;
return $date.' '.$time_in_actual.':00:00';
}
}
Something like this should do.
list($hour, $minute) = explode(':', $time);
if ($minute > 30) {
$hour++;
$minute = 0;
} elseif ($minute > 0) {
$minute = 30;
}
if ($hour == 24) $hour = 0;
printf('%02d:%02d', $hour, $minute);
function military_time($x){
$y=split(':',$x);
$y[0] =(((int)$y[1]) >30) ? ((int)$y[0])+1 : $y[0];
$y[0] =((int) $y[0] == 24) ? '00':$y[0];
$y[0]=(strlen($y[0]) == 1) ? '0'.$y[0] : $y[0];
$y[1] =(((int)$y[1]) >30) ? '00':'30';
echo $y[0].':'.$y[1];
}
military_time('23:35');
Just add 30 minutes and then round the number DOWN to the closest :30 or :00.