Speeding up PHP script - php

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

Related

Trying to show different banner images based on server time using PHP

I have four greeting images which I intend to show based on what time user enters the site.
$morning = "img/morning.png";
$afternoon = "img/afternoon.png";
$evening = "img/evening.png";
$night = "img/night.png";
And I have some conditional statements to assign the values to $cover variable. When I tested, the conditional statement doesn't work.
date_default_timezone_set('Asia/Yangon');
$Hour = date('G');
if($Hour >= 5 && $Hour <= 11) {
$cover = $morning;
}elseif ($Hour >= 11 && $Hour <= 4) {
$cover = $afternoon;
}elseif ($Hour >= 4 && $Hour <= 9){
$cover = $evening;
}elseif ($Hour >= 9 && $Hour <= 4) {
$cover = $night;
}
Img tag
<img class="card-img-top" src="<?php echo $cover; ?>" alt="Not Available" >
if($Hour >= 0 && $Hour <= 11) {
$cover = $morning;
}
elseif ($Hour > 11 && $Hour <= 16) {
$cover = $afternoon;
}
elseif ($Hour > 16 && $Hour <= 19){
$cover = $evening;
}
else{
$cover = $night;
}
Above code will check your hours from 00:00 until 24:00 next day. I fixed your if-else statements so they make more sense in a way that there is a flow in the times.
G 24-hour format of an hour without leading zeros 0 through 23
$hour = date('H', time());
if( $hour > 6 && $hour <= 11) {
$cover = $morning;
}
else if($hour > 11 && $hour <= 16) {
$cover = $afternoon;
}
else if($hour > 16 && $hour <= 23) {
$cover = $evening;
}
else {
$cover = $night;
}
Construct an instance of date and acquire the hour from it

How to print "good morning/afternoon/evening" depending on the time of day?

The code below is meant to show my visitors something like "Good Morning, Today is ------ and the time is -----"
But I keep getting "Welcome" instead of the main greetings. Why is this?
<?php
$hour = date ("G");
$minute = date ("i");
$second = date ("s");
$msg = " Today is " . date ("l, M. d, Y.") . " And the time is " . date ("g:i a");
if ($hour = 00 && $hour <= 9 && $minute <= 59 && $second <= 59)
{ $greet = "Good Morning,"; }
else {
if ($hour >= 10 && $hour <= 11 && $minute <= 59 && $second <= 59)
{ $greet = "Good Day,"; }
if ($hour >= 12 && $hour <= 15 && $minute <= 59 && $second <= 59)
{ $greet = "Good Afternoon,"; }
if ($hour >= 16 && $hour <= 23 && $minute <= 59 && $second <= 59)
{ $greet = "Good Evening,"; }
else { $greet = "Welcome,"; }
}
echo $greet.$msg;
?>
Any idea on how to get this done?
How do I get timezones of my visitors automatically using php. My server's default timezone is Lagos/Africa. I want the code above to show my visitors their local time and date.
Your problem is here
if ($hour = 00 && $hour <= 9 && $minute <= 59 && $second <= 59)
this way you assign 00 to $hour and all the other condition fails
you need
if ($hour == 0 && $hour <= 9 && $minute <= 59 && $second <= 59)
solved the if problem there are others
You should keep in mind that php date() function return a string
there are several way for solve your code problem these are just some suggestion
so you should commpare string (not number) or convert the string in number for commparision using a proper cast
Looking to your if you can also avoid the check for minute ( are always <= 59) this mean that you code could be
<?php
$hour = date("G");
$minute = date("i");
$second = date("s");
$msg = " Today is " . date("l, M. d, Y.") . " And the time is " . date("g:i a");
if ( (int)$hour == 0 && (int)$hour <= 9 ) {
$greet = "Good Morning,";
} else if ( (int)$hour >= 10 && (int)$hour <= 11 ) {
$greet = "Good Day,";
} else if ( (int)$hour >= 12 && (int)$hour <= 15 ) {
$greet = "Good Afternoon,";
} else if ( (int)$hour >= 16 && (int)$hour <= 23 ) {
$greet = "Good Evening,";
} else {
$greet = "Welcome,";
}
echo $greet.$msg;
?>
try this....
<?php
$hour = date ("G");
$minute = date ("i");
$second = date ("s");
$msg = " Today is " . date ("l, M. d, Y.") . " And the time is " . date ("g:i a");
if ($hour == 00 && $hour <= 9 && $minute <= 59 && $second <= 59){
$greet = "Good Morning,";
}else if ($hour >= 10 && $hour <= 11 && $minute <= 59 && $second <= 59){
$greet = "Good Day,";
}else if ($hour >= 12 && $hour <= 15 && $minute <= 59 && $second <= 59){
$greet = "Good Afternoon,";
}else if ($hour >= 16 && $hour <= 23 && $minute <= 59 && $second <= 59){
$greet = "Good Evening,";
}else {
$greet = "Welcome,";
}
}
echo $greet.$msg;
?>
I recommend you read some tuts

php - how to check if a date is in a given season (regardless of the current year)?

High Season:
April 28 - September 30,
December 27 - January 3
Low Season:
October 1 - December 26,
January 4 - April 27
I gave a date to check: 2014-02-18 and I want to have TRUE or FALSE in case of which season includes it. How to do it regardless of the current year?
Try with simple date comparing:
function is_high_season($date) {
$md = gmdate('m-d', strtotime($date));
return
('03-28' <= $md && $md <= '09-30') ||
('12-27' <= $md && $md <= '12-31') ||
('01-01' <= $md && $md <= '01-03');
}
demo
$day = $d->format('j');
$month = $d->format('n');
if($month == 1 && $day <= 3) {
return 'high';
} elseif $month < 4 || ($month == 4 && $day < 28)) {
return 'low';
} elseif($month == 4 && $day >= 28) {
return 'high';
} elseif($month < 10) {
return 'high';
} elseif($month < 12 || ($month == 12 && $day < 27)) {
return 'low';
} elseif($month == 12 && $day >= 27) {
return 'high';
}
$Date = date('Y-m-d');
$Date=date('Y-m-d', strtotime($Date));;
//echo $Date; // echos today!
$hiDateBegin = date('Y-m-d', strtotime("28/04/2014"));
$hiDateEnd = date('Y-m-d', strtotime("30/09/2014"));
$hiDateBegin2 = date('Y-m-d', strtotime("27/12/2014"));
$hiDateEnd2 = date('Y-m-d', strtotime("03/01/2015"));
$lowDateBegin = date('Y-m-d', strtotime("01/10/2014"));
$lowDateEnd = date('Y-m-d', strtotime("26/12/2014"));
$lowDateBegin2 = date('Y-m-d', strtotime("04/01/2015"));
$lowDateEnd2 = date('Y-m-d', strtotime("27/04/2015"));
if (($Date > $hiDateBegin) && ($Date < $hiDateEnd))
{
echo "is Hi Season";
}
if (($Date > $hiDateBegin2) && ($Date < $hiDateEnd2))
{
echo "is Hi Season";
}
if (($Date > $lowDateBegin) && ($Date < $lowDateEnd))
{
echo "is Low Season";
}
if (($Date > $lowDateBegin2) && ($Date < $lowDateEnd2))
{
echo "is Low Season";
}
else
{
echo "Date doesn't fall in a season!";
}

How to convert military time to a whole time in PHP?

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.

php return date/time issue [duplicate]

This question already has answers here:
How to get time difference in minutes in PHP
(21 answers)
Closed 9 years ago.
I have a simple function below that gets seconds and returns it like "2 minutes ago", "9 hours ago" etc...
if I pass 1 on 'relativedate()' function it will just return 1 sec and so on. As we all know 60*60*24*7*30 or 18144000sec = 1 month. Therefore if I pass a value of 18144000 on the parameter, it should return 1 month.However, if I pass a value on the parameter lets say 231440000 on relativedate() which should be more than a year, it is returning '13 months' instead of 1 year.
function relativedate($secs) {
$second = 1;
$minute = 60;
$hour = 60*60;
$day = 60*60*24;
$week = 60*60*24*7;
$month = 60*60*24*7*30;
$year = 60*60*24*7*30*365;
if ($secs <= 0) { $output = "now";
}elseif ($secs > $second && $secs < $minute) { $output = round($secs/$second)." second";
}elseif ($secs >= $minute && $secs < $hour) { $output = round($secs/$minute)." minute";
}elseif ($secs >= $hour && $secs < $day) { $output = round($secs/$hour)." hour";
}elseif ($secs >= $day && $secs < $week) { $output = round($secs/$day)." day";
}elseif ($secs >= $week && $secs < $month) { $output = round($secs/$week)." week";
}elseif ($secs >= $month && $secs < $year) { $output = round($secs/$month)." month";
}elseif ($secs >= $year && $secs < $year*10) { $output = round($secs/$year)." year";
}else{ $output = " more than a decade ago"; }
if ($output <> "now"){
$output = (substr($output,0,2)<>"1 ") ? $output."s" : $output;
}
return $output;
}
echo relativedate(60); // 1 minute
Your year calculation is for 365 Months, not 12 months

Categories