Counting number of working days and weekends using PHP/ MySQL - php

I am looking to calculate the number of weekdays worked and the number of weekend days worked from data i retrieve out of a MySQL database.
The time from the DB is formatted like this : 2013-07-01 07:00
This is what I have so far:
function isWeekend($date) {
$check = date("w", strtotime($date));
if ($check == 6 || $check == 0) {
return 1;
}
else {
return 0;
}
}
$query = mysql_query ("SELECT date from jobcards");
while ($row = mysql_fetch_assoc($query)) {
$date = $row['date'];
$date_check= isWeekend($date);
if ($date_check == 1) {
++$weekend;
}
else {
++$workday;
}
}
I need to find a way to count the days using mysql instead, is there such a way or a more elegant way to improve the PHP code ?
Also if I have multiple records in the database with the same date range but a different time example: 2013-07-01 07:00 and 2013-07-01 07:30 it will be counted as two workdays, how would i prevent that ?
Thanks.

Have a look at the WEEKDAY function, and compare it to 6/5 for Sunday/Saturday (respectively)
Your SQL will look something like:
SELECT SUM(IF(WEEKDAY(date) >= 5, 1, 0) AS WeekendCount,
SUM(IF(WEEKDAY(date) < 5, 1, 0) AS WeekdayCount
FROM jobcards
There is a similar answer here: MySQL Weekday/Weekend count - Part II
Fixed the ) of the IF being placed in the wrong place

SELECT SUM(IF(DAYOFWEEK(date) BETWEEN 2 AND 6,1,0) AS weekdays,
SUM(IF(DAYOFWEEK(date) NOT BETWEEN 2 AND 6,1,0) AS weekends,
FROM jobcards

SELECT date,
sum(date) total,
sum(if(date_format(date,'%w') between 1 and 5),1,0) weekday,
sum(date) - sum(if(date_format(date,'%w') between 1 and 5),1,0) weekend
from jobcards

If you want to do this in SQL, you can solve the timestamp problem using the MySQL DATE() function, and the DAYOFWEEK() function to count your weekdays/weekends (Note that the day numbers for Sat/Sun are 7/1 in MySQL and not 6/0 as in PHP). So to count the distinct weekday entries it would look something like:
SELECT COUNT(*) FROM jobcards WHERE DAYOFWEEK(DATE(jobcards.date)) BETWEEN 2 AND 6;

Related

Calculating frequency interval for tasks

I have a cron job that gets results from the DB to check it the interval set by user falls on today's date. I am currently thinking of doing it as below :
Get the time column for the row. Ex:2017-05-25 00:00:00
Get the frequency set. Ex:Every 2 weeks.
Get the current date in above format. Ex:2017-05-31 00:00:00
Get the difference in days. Ex:6 days.
Convert the frequency set to days. Ex:2 weeks = 14 days;
Divide (difference in time(days)) by (frequency in days). Ex:6/14
This way I will only get the result to be true when 2 weeks have passed since the time set. I.e., 14/14, 28/14, 42/14,...
If the frequency is in months, I can start dividing by 30. But somehow this feels like a hacky way of doing it. So my question is if there is better way of doing this calculation to check the difference.
This is what I have done as explained by above example.
` $frequency = ; // Get the relevant fields from db
$today = date(Y-m-d H:i:s);
foreach ($frequency as $key => $value) {
$frequency_in_days;
$frequency_type = $value->type;
$frequency_repeat = $value->repeat;
if($frequency_type == 1){
$frequency_in_days = $frequency_repeat;
} elseif($frequency_type == 2) {
$frequency_in_days = $frequency_repeat * 7;
} elseif($frequency_type == 3) {
$frequency_in_days = $frequency_repeat * 30;
} elseif($frequency_type == 4) {
$frequency_in_days = $frequency_repeat * 365;
}
// Get number of days spent between start_date and today in days.
$interval = date_diff($value->start_date, $today)->format('%a');
$result = $interval % $frequency_in_days;
if ($result == 0) {
// Frequency falls today! Do the job.
}
}`
Note: The cron job runs this script. The script again needs to check if the today falls under the frequency set.
Also for argument's sake, is this the best logic to calculate the difference?
Thank you.
This will work
Table "schedule"
`last_run` timestamp,
`frequency_seconds` int
example query for tasks that should go every two weeks:
SELECT *
FROM schedule
WHERE TIMESTAMPDIFF(last_run, NOW()) >= frequency_seconds
after fetching rows update last_run to NOW()

Complex date format

I am trying to come up with the most efficient and best way to accomplish this somewhat of a complex situation. I know that I could build this solution using probably around 5 if else statements, maybe more - however there must be a better way to accomplish what I want to.
So here's what I am trying to do. I have an events page on my website, and what I want to do is display the dates in a minimalistic way when possible. What I mean is the following:
Say I have 3 dates: May 5, May 6, May 7. I want to display it as: May 5 - 7.
However, there will be situations where the dates may be: May 5, May 7. In this case I would like to display it as: May 5 & 7.
However, there may also be situations where the dates may be: May 25, June 2. In this case I would like to display it as: May 25 & June 2.
However! There also may be situations where the dates may be: May 25, May 26, June 2. In this case it should display as: May 25 - 26 & June 2
Of course, there could just be a single date as well. But one other thing, it could be possible that there could be more than 3 dates as well, so it would be nice if it could work regardless of how many dates there are (IE loop through an array).
I know that we are suppose to make an attempt and show some code to debug, however I don't even know where to start with this, if this is too much for someone to put together - just giving me an idea of how to do something like this efficiently would be a huge help.
Thanks
//input data: sorted list of dates
$dates = array('May 5','May 6','May 7','May 30','Jun 2','Jun 3','Dec 11','Dec 12','Dec 14');
array_push($dates,false); //add an extra value so the last range gets printed
//initialize min & previous date as first date
$min_date = array_shift($dates);
$prev_date = $min_date;
$counter = 0; //keep count of # of days between min and max
$formatted_dates = array();
foreach($dates as $date) {
//if the difference in number of days between current date and min date
//is greater than counted number of days then we capture the current range
//and start a new one by resetting $min_date to $date and $counter to 0
if(!$date || ($counter + 1) < diff_in_days($min_date,$date)) {
if($counter == 0) { //format for 1 date
$formatted_dates[] = $min_date;
}
elseif($counter == 1) { //format for 2 dates
$formatted_dates[] = "$min_date & $prev_date";
}
elseif($counter > 1) { //format for > 2 dates
$formatted_dates[] = "$min_date - $prev_date";
}
$counter = 0;
$min_date = $date;
}
else {
$counter++;
}
$prev_date = $date;
}
//may also want to verify that neither formatted date contains an '&'
//so you don't end up with "May 11 & May 12 & June 1 & June 2" which may be confusing
if(count($formatted_dates) == 2) {
print implode(' & ',$formatted_dates);
}
else {
print implode("\n",$formatted_dates);
}
function diff_in_days($day1,$day2) {
$datetime1 = new DateTime($day1);
$datetime2 = new DateTime($day2);
$interval = $datetime1->diff($datetime2);
$ret = (int) $interval->format('%a');
return $ret;
}
Output
May 5 - May 7
May 30
Jun 2 & Jun 3
Dec 11 & Dec 12
Dec 14

upper limit for active records in codeigniter

I have this function in my model the purpose of it is to get the stats of the past 30 days starting from the actual day, it was working fine before i reach 30th day then I found out it is counting from the oldest date, so I flipped the order_by from "ase" to "desc" but it seems still going back and counting before the oldest day and not giving me that data I want, so I'm wondering if there any way using codeigniter and give a starting point for the "limit" which should be the actual date.
function graph_data($id_person)
{
$this->db->limit(30); // get data for the last 30 days including the curent day
$this->db->where('personid',$id_person);
$this->db->order_by('date', 'ase');
$query = $this->db->get('stats');
foreach($query-> result_array() as $row)
{
$data[] = $row;
}
return $data;
}
limit returns in your case the last 30 rows. It has nothing to do with dates, unless you only add a row each day.
try using mysql Between
SELECT DATE_FORMAT(create_date, '%m/%d/%Y')
FROM mytable
WHERE create_date BETWEEN SYSDATE() - INTERVAL 30 DAY AND SYSDATE()
ORDER BY create_date ASC
Source -- https://stackoverflow.com/a/2041619/337055
I finally found the solution by giving a range or days, 29 days before the actual day without using BETWEEN and that works fine
<?php
function graph_data($id_person)
{
$today = date('Y-m-d');
$lastdate = date('Y-m-d', strtotime('-29 days', strtotime($today)));// 30 days ago
$this->db->where('personid',$id_person);
$this->db->where("date <= '$today'");
$this->db->where("date >= '$lastdate'");
$this->db->order_by('date', 'ase');
$query = $this->db->get('stats');
$data = array();
foreach($query-> result_array() as $row)
{
$data[] = $row;
}
return $data;
}

sql php confuse complex

PHP code (relevant)
$db = '2011-02-28'; $a = '2011-02-01';
and this is the part of the query
LEFT JOIN
abc
ON
abc.date between '$a' and '$db'
This query show following results :
1 2011-02-08
6 2011-02-09
6 2011-02-11
1 2011-02-13
but what i want is to get 0 as a result if there's n rows for other dates.
You can't do that (not without joining a table containing all dates, at least).
Displaying the 0s for dates with no data is the job of your application, not the database. It's trivial, just write a loop from the start date to the end date, and output the 0 for those dates with no rows in your result set.
SQL's job is to tell you what data is there, not what isn't.
while ($row = mysql_fetch_array($result)) {
$results[$row['date']] = $row['count'];
}
for ($time = strtotime('2011-02-01'); $time <= strtotime('2011-02-28'); $time += 86400) {
$datestr = date('Y-m-d', $time);
if (isset($results[$datestr])) {
echo "Count for date $datestr: " . $results[$datestr];
} else {
echo "Count for date $datestr: 0";
}
}
From the limited information you give about what problem you are trying to solve I would guess you are trying to find "free appointments" or similar.
If so then this should help you out. http://www.artfulsoftware.com/infotree/queries.php?&bw=1280#98
This website has a number of query patterns that will help your thinking and design of your database.

Displaying Scheduled Events

I had this problem some years ago and back then I implemented a "different logic" in order to deliver the project but the doubt remains in my mind and hopefully with your help I'll be able to understand it now.
Suppose I have some scheduled events on my database that may or may not spawn over several days:
id event start end
-----------------------------------------------
1 fishing trip 2009-12-15 2009-12-15
2 fishCON 2009-12-18 2009-12-20
3 fishXMAS 2009-12-24 2009-12-25
Now I wish to display the events in a calendar, lets take the month of December:
for ($day = 1; $day <= 31; $day++)
{
if (dayHasEvents('2009-12-' . $day) === true)
{
// display the day number w/ a link
}
else
{
// display the day number
}
}
What query should the dayHasEvents() function do to check if there are (or not) events for the day? I'm guessing SELECT .. WHERE .. BETWEEN makes the most sense here but I've no idea how to implement it. Am I in the right direction?
Thanks in advance!
#James:
Lets say we're on December 19th:
SELECT *
FROM events
WHERE start >= '2009-12-19 00:00:00'
AND end <= '2009-12-19 23:59:59'
Should return the event #2, but returns nothing. =\
You should scratch that approach and grab all events for the given month up front so you only need to perform a single query as opposed to N queries where N is the number of days in the month.
You could then store the returned results in a multidimensional array like so:
// assume event results are in an array of objects in $result
$events = array();
foreach ($result as $r) {
// add event month and day as they key index
$key = (int) date('j', strtotime($r->start));
// store entire returned result in array referenced by key
$events[$key][] = $r;
}
Now you'll have a multidimensional array of events for the given month with the key being the day. You can easily check if any events exist on a given day by doing:
$day = 21;
if (!empty($events[$day])) {
// events found, iterate over all events
foreach ($events[$day] as $event) {
// output event result as an example
var_dump($event);
}
}
You're definitely on the right track. Here is how I would go about doing it:
SELECT *
FROM events
WHERE start <= '2009-12-01 00:00:00'
AND end >= '2009-12-01 23:59:59'
And you obviously just replace those date values with the day you're checking on.
James has the right idea on the SQL statement. You definitely don't want to run multiple MySQL SELECTs from within a for loop. If daysHasEvents runs a SELECT that's 31 separate SQL queries. Ouch! What a performance killer.
Instead, load the days of the month that have events into an array (using one SQL query) and then iterate through the days. Something like this:
$sql= "SELECT start, end FROM events WHERE start >= '2009-12-01' AND end <= '2009-12-31'";
$r= mysql_query($sql);
$dates= array();
while ($row = mysql_fetch_assoc($r)) {
// process the entry into a lookup
$start= date('Y-m-d', strtotime($row['start']));
if (!isset($dates[$start])) $dates[$start]= array();
$dates[$start][]= $row;
$end= date('Y-m-d', strtotime($row['end']));
if ($end != $start) {
if (!isset($dates[$end])) $dates[$end]= array();
$dates[$end][]= $row;
}
}
// Then step through the days of the month and check for entries for each day:
for ($day = 1; $day <= 31; $day++)
{
$d= sprintf('2009-12-%02d', $day);
if (isset($dates[$d])) {
// display the day number w/ a link
} else {
// display the day number
}
}
For your purposes a better SQL statement would be one that grabs the start date and the number of events on each day. This statement will only work properly if the start column is date column with no time component:
$sql= "SELECT start, end, COUNT(*) events_count FROM events WHERE start >= '2009-12-01' AND end <= '2009-12-31' GROUP BY start, end";

Categories