Group data by year, month, week and day - php

I have a MySQL database with sales records, what I want to do is to generate a JSON which shows all the dates between the oldest date and the most recent, and then group that info by year, month, week and day, each day will show a number (representing the quantity sold that day), if there's no data matching that date, the number should be 0.
I'm actually using PHP to solve this problem. I've tried so many solutions, but none of them seems to work.
Here's a screenshot of my query getting executed on PHPMyAdmin: https://prnt.sc/pe4twq
The final JSON should be something like this:
[
{
"year":"2019",
"months":{
"January":{
"week 1":{
"day 3":{
"total":"3400"
}
}
}
}
}
]
EDIT: Here's some code that I've tried
foreach ($rows as $row):
$date = $row['date'];
$dates[] = $date;
$first = reset($dates);
$first = strtotime($first);
$last = end($dates);
$last = strtotime($last);
$year = date('Y', strtotime($date));
$years[] = $year;
endforeach;
//86400 = 24 hours
for($i = $first; $i <= $last; $i+=86400) {
$dt = date('Y-m-d', $i);
$y = date('Y', strtotime($dt));
$m = date('n', strtotime($dt));
$w = date('W', strtotime($dt));
$d = date('D', strtotime($dt));
$allYears[] = $y;
if (in_array($dt, $dates)) {
$result[] = ['year' => $y, 'month' => $meses[$m], 'week' => $w, 'day' => $dias[$d], 'total'=>$row["total"]];
} else {
$result[] = ['year' => $y, 'month' => $meses[$m], 'week' => $w, 'day' => $dias[$d], 'total'=>"0"];
}
}
echo json_encode($result);
What I do here is I generate all the dates between the first and the last year on my database, and then I assign the total to each date, generating a JSON object for every date of every year: http://prntscr.com/pe4yjj

Related

Generate Array of Workdays with working hours for each day

Hello I am trying to generate array which calculates all days of each month and then subtract the weekends, and after that I would like to add 8 hours for each day, so 1 = 8 , 2 = 16 etc. So far I have been able to generate the array with all days of specific month. Trying to subtract the weekends and add 8 hours for each day but no luck so far.
Here is my code:
<?php
$list=array();
$month = 1;
$year = 2017;
for($d=1; $d<=31; $d++)
{
$time=mktime(12, 0, 0, $month, $d, $year);
if (date('m', $time)==$month && ((date('D', $time) != 'Sat' || (date('D', $time) != 'Sun'))))
$list[]=date('d', $time);
}
echo "<pre>";
print_r($list);
echo "</pre>";
?>
So the final result should be something like this:
Array
(
[0][0] => 01
[0][1] => 8
[1][0] => 02
[1][1] => 16
...
)
Basically the goal is to calculate each workday how many hours should contains for a month. After having this information will be able to compare it with today date and see if working hours are completed for this month or not.
Any help is very welcome.
One way you could achieve what your after is to use DateTime to keep track of the date:
$year = 2017;
$month = 1;
$date = (new Datetime())->setDate($year, $month, 1);
$days = [];
$hours = 0;
foreach (range(1, $date->format('t')) as $i) {
$hours += $date->format('N') < 6 ? 8 : 0;
$days[] = [
'date' => $date->format('d'),
'hours' => $hours,
];
$date->modify('+1 day');
}
This is what is going on:
Set the DateTime to be the beginning of the month.
foreach(range(...)) s just another way to write for($i=1;$i<=$date->format('t');$i++). t will just give you the days in the month.
$date->format('N') < 6 is check if the day of the week is before Saturday. If so increment the $hours count by 8.
I think the rest is fairly straightforward.
Hope this helps!

Get all occurrence of specific day in a month

Suppose i have a month June 2014. Now i want to get dates of all Mondays in June month.
like Monday is coming on following days so answer will be like following
2014-06-02
2014-06-09
2014-06-16
2014-06-23
2014-06-30
please do not give be static solution only for June. I need dynamic solution for every month and purely in PHP.
Try this -
<?php
$startDate = "2014-06-01";
$endDate = "2014-06-30";
$startDate = strtotime($startDate);
$endDate = strtotime($endDate);
for($i = strtotime('Monday', $startDate); $i <= $endDate; $i = strtotime('+1 week', $i))
echo date('l Y-m-d', $i).PHP_EOL;
DEMO:
http://3v4l.org/n4ULA
Try to create an array with all your date with day on key (with variable $day and $date):
$array = array("Monday" => "2014-06-02", "Tuesday" => "2014-06-03", "Wednesday" => "2014-06-04");
You create a loop to reach all the result :
foreach($array as $key => $value {
if($key == "Monday")
echo $value;
}
Using the above, I created this so you can use variables to define the month, year, and selected weekday.
$month = "6";
$year = "2022";
$weekday = "Tuesday";
$d=cal_days_in_month(CAL_GREGORIAN,$month,$year);
$first_date_of_month = $year."-".$month."-01";
$last_date_of_month = $year."-".$month."-".$d;
$startDate = strtotime($first_date_of_month);
$endDate = strtotime($last_date_of_month);
for($i = strtotime($weekday, $startDate); $i <= $endDate; $i = strtotime('+1 week', $i))
echo "<br />". date('l Y-m-d', $i).PHP_EOL;

How to sum time in php?

PHP: How to sum the result of a foreach statement?
Below is my working code. As you will see, I'm trying to count the days in each month and then sum them.
// A function to calculate days in a given month using just the date
function daysInMonths($date)
{
$month = date("m", strtotime($date));
$year = date("Y", strtotime($date));
$num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
return $num;
}
// A function that places the date of an unknown number of months into an array:
function getNextMonths($date, $numberOfMonths)
{
$timestamp_now = strtotime($date);
$months[] = date('Y-m-d', $timestamp_now);
for($i = 1;$i <= $numberOfMonths; $i++)
{
$months[] = date('Y-m-d', (strtotime($months[0].' +'.$i.' month')));
}
// counts the days in each month:
$j=0;
foreach ($months as $days)
{
echo "$j:".daysInMonths($days)."<br>";
++$j;
}
print_r($months);
}
getNextMonths('2011-11-1', '4');
Current output:
Array (
[0] => 2011-11-01
[1] => 2011-12-01
[2] => 2012-01-01
[3] => 2012-02-01
[4] => 2012-03-01
)
After counting:
0:30
1:31
2:31
3:29
4:31
This is all correct, I'm just having trouble summing the array after I have the days of the month counted.
$tot = 0;
foreach ($months as $days) {
$tot += daysInMonths(days);
}
echo $tot;
array_sum() - http://php.net/manual/en/function.array-sum.php
$t = array(
31,
28,
31
);
echo array_sum($t); // 90
Add a counter like you use in for while etc!!

PHP- Display days weekly by giving 2 dates

I'd like display dates by week number between giving 2 dates like example below. Is this possible in PHP?
if the dates are 2010-12-01 thru 2010-12-19, it will display it as follows.
week-1
2010-12-01
2010-12-02
2010-12-03
2010-12-04
2010-12-05
2010-12-06
2010-12-07
week-2
2010-12-08
2010-12-09
2010-12-10
2010-12-11
2010-12-12
2010-12-13
2010-12-14
week-3
2010-12-15
2010-12-16
2010-12-17
2010-12-18
2010-12-19
and so on...
I use mysql. It has startdate end enddate fields.
thank you in advance.
I can get how many weeks in those giving 2 dates and display them using a
datediff('ww', '2010-12-01', '2010-12-19', false); I found on the internet.
And I can display dates between two dates as follows. But I am having trouble grouping them by week.
$sdate = "2010-12-01";
$edate = "2010-12-19";
$days = getDaysInBetween($sdate, $edate);
foreach ($days as $val)
{
echo $val;
}
function getDaysInBetween($start, $end) {
// Vars
$day = 86400; // Day in seconds
$format = 'Y-m-d'; // Output format (see PHP date funciton)
$sTime = strtotime($start); // Start as time
$eTime = strtotime($end); // End as time
$numDays = round(($eTime - $sTime) / $day) + 1;
$days = array();
// Get days
for ($d = 0; $d < $numDays; $d++) {
$days[] = date($format, ($sTime + ($d * $day)));
}
// Return days
return $days;
}
New answer.
$current_date = strtotime('2010-12-01');
$end_date = strtotime('2010-12-19');
$day_count = 0;
$current_week = null;
do {
if ((int)($day_count / 7) + 1 != $current_week) {
$current_week = (int)($day_count / 7) + 1;
echo 'week-'.$current_week.'<br />';
}
echo date('Y-m-d', $current_date).'<br />';
$current_date = strtotime('+1 day', $current_date);
$day_count ++;
} while ($current_date <= $end_date);
You will definitely need this: Simplest way to increment a date in PHP?. Write a forloop and increment the day every time. You will also need the DateTime class and functions as date. Indeed asking for date('W', yourDateHere) is a nice idea.
You will get something like this (pseudocode)
$startDate;
$endDate;
$nrOfDays = dateDiffInDays($endDate, $startDate);
$currentWeek = date('W',$startDate);
for($i = 0; $i < $nrOfDays; $i++)
{
$newDay = date('+$i day', $startDate); // get the incremented day
$newWeek = date('W', $newDay); // get the week of the new day
if($newWeek != $currentWeek) // check if we must print the new week, or if we are still in the current
print $newWeek;
print $newDay; // print the day
}
Hope this helps. Good luck.
Tools sufficient to do the job:
strtotime('2010-11-23') - to get a timestamp from a date
strtotime('+1 day', $someTimestamp) - to get the next day
date('W', $someTimestamp) - to get the week number (if you want to group by ISO week number)
array_chunk($orderedListOfSuccessiveDates, 7) - to split in groups of seven days (if you don't want to group by ISO week number)
Warning: Never, ever increment days by adding 86400 to the timestamp! That is the easiest way to break everything when Daylight Saving comes along. Either use the strtotime function or the DateTime class.
Here you go. Although this is with weeks starting on sundays (just change it to monday if need be). And it doesnt work if the dates arent in the same year. But it should be pretty easy to fix that. If not_same_year then ...
$start_date = mktime(0, 0, 0, 12, 01, 2010);
$start_date_week_number = (int) date("W", $start_date);
$end_date = mktime(0, 0, 0, 12, 19, 2010);
$end_date_week_number = (int) date("W", $end_date);
$n = $start_date_week_number;
$w = 1;
$date = $start_date;
while($n <= $end_date_week_number) {
echo("<strong>Week " . $w . "</strong><br />");
$s = 0;
$e = 6;
if($n == $start_date_week_number) $s = (int) date("w", $start_date);
elseif($n == $end_date_week_number) $e = (int) date("w", $end_date);
while($s <= $e) {
echo(date("j-m-y", $date) . "<br />");
$c_date = getdate($date);
$date = mktime($c_date['hours'], $c_date['minutes'], $c_date['seconds'], $c_date['mon'], $c_date['mday'] + 1, $c_date['year']);
$s++;
}
$n++; $w++;
}
DEMO HERE
Edit: just fixed it when I realized you wanted to count the weeks (not get the actual week number)...
$startDate = new DateTime('2010-01-01');
$endDate = new DateTime('2010-01-14');
$weeksDays = getWeeksDaysBetween($startDate, $endDate);
foreach($weeksDays as $week => $days)
{
echo "Week $week<ul>";
foreach($days as $day){
echo "<li>$day</li>";
}
echo "</ul>";
}
function getWeeksDaysBetween($startDate, $endDate)
{
$weeksDays = array();
$dateDiff = $endDate->diff($startDate);
$fullDays = $dateDiff->d;
$numWeeks = floor($fullDays / 7) + 1;
$weeksDays[1][] = $startDate->format('Y-m-d');
for ($i = 1; $i <= $fullDays; $i++)
{
$weekNum = floor($i / 7) + 1;
$dateInterval = DateInterval::createFromDateString("1 day");
$weeksDays[$weekNum][] = $startDate->add($dateInterval)->format('Y-m-d');
}
return $weeksDays;
}

PHP: get next 13 dates from date?

I am trying to get an array of a date plus the next 13 dates to get a 14 day schedule starting from a given date.
here is my function:
$time = strtotime($s_row['schedule_start_date']); // 20091030
$day = 60*60*24;
for($i = 0; $i<14; $i++)
{
$the_time = $time+($day*$i);
$date = date('Y-m-d',$the_time);
array_push($dates,$date);
}
But it seems to be repeating a date when the month switches over..
this is what I get:
2009-10-30|2009-10-31|2009-11-01|2009-11-01|2009-11-02|2009-11-03|2009-11-04|2009-11-05|2009-11-06|2009-11-07|2009-11-08|2009-11-09|2009-11-10|2009-11-11
Notice that 2009-11-01 is repeated. I cannot figure out why?
What am I doing wrong?
Thanks!!
I would use strtotime
$start = strtotime($s_row['schedule_start_date']);
$dates=array();
for($i = 1; $i<=14; $i++)
{
array_push($dates,date('Y-m-d', strtotime("+$i day", $start)));
}
print_r($dates);
You have the same date because of daylight saving time switch. It's not safe to add 24*60*60 seconds to find next day, because 2 days in the year have more/less seconds in them. When you switch from summer to winter time you are adding 1 hour to a day. So it'll be 25*60*60 seconds in that day, that's why it's not switched in your code.
You can do your calculation by mktime(). For example:
## calculate seconds from epoch start for tomorrow
$tomorrow_epoch = mktime(0, 0, 0, date("m"), date("d")+1, date("Y"));
## format result in the way you need
$tomorrow_date = date("M-d-Y", $tomorrow_epoch);
Or the full version for your code:
$dates = array();
$now_year = date("Y");
$now_month = date("m");
$now_day = date("d");
for($i = 0; $i < 14; $i++) {
$next_day_epoch = mktime(0, 0, 0, $now_month, $now_day + $i, $now_year);
array_push(
$dates,
date("Y-m-d", $next_day_epoch)
);
}
I recommend something like:
for($i=1;$i<=14;$i++){
echo("$i day(s) away: ".date("m/d/Y",strtotime("+$i days")));
}

Categories