I am trying to output the count of total registrations today, registrations in the last 7 days, last 30 days.
All the other counts are output as expected. Except for registrations today.
$registration_summery = [
'Registrations today' => '+0 days',
'Registrations in the last 7 Days' => '-7 days',
'Registrations in the last 15 Days' => '-15 days',
'Registrations in the last 30 Days' => '-30 days',
'Registrations in the last 6 month' => '-6 months',
'Registrations in the last 1 year' => '-1 Years'
];
$toReturn['registration_summery'][] = ['name' => 'Total Learners', 'value' => \DB::table('users')->where('users.role', 'student')->count()];
foreach ($registration_summery as $key => $value) {
$tmp = \DB::table('users')->where('role', 'student')
->where('createdAt', '>=', strtotime("tomorrow", strtotime($value)) - 1)
->where('createdAt', '<=', strtotime("midnight"))
->count();
$toReturn['registration_summery'][] = ['name' => $key, 'value' => $tmp];
}
No Errors, but the count shows 0
You can achieve that by substracting 23 hours more like this.
$registration_summery = [
'Registrations today' => '+0 days -23Hours',
// rest data.....
Fixed this.
Changed line 2 to -1 days.
$registration_summery = ['Registrations today' => '-1 days', 'Registrations in the last 7 Days' => '-7 days',
and changed "midnight" to "tomorrow"
->where('createdAt', '<=', strtotime("tomorrow"))
Thank you #rahul for your help
Related
I want to display the start date and end date of the week. I have One date and a string like 1W4 and,in 1W4 consider 4 weeks and 1 visit so, my string like this 2W4,1W2,3W3,1W1,2W4.
I want to make start date and end date of week array according to string and week start from Sunday to Saturday.
Please post me if anyone has solution.Please ignoring if mistake in asking Question.
Thank you.
Try my php code:
From php.net datetime.format:
W: ISO-8601 week number of year, weeks starting on Monday.
The first calendar week of a year is that one which includes the first Thursday of that year.
So I have to rest one day to the start week date.
I assumed that the weeks correspond to the current year.
Input string:
$weeksString = "2W4,1W2,3W3,1W1,2W4";
Code:
<?php
$weeksArray = explode(",", $weeksString);
$result = array();
foreach($weeksArray as $visitsWeek) {
list($visits, $week) = explode("W", $visitsWeek);
$startDate = date("Y-m-d", strtotime(date("Y") . "W" . str_pad($week, 2, "0", STR_PAD_LEFT) . " -1 days"));
$endDate = date("Y-m-d", strtotime($startDate . " +6 days"));
$result[] = array("week" => $week, "startDate" => $startDate, "endDate" => $endDate);
}
?>
Output array $result:
array ( 0 => array ( 'week' => '4', 'startDate' => '2021-01-24', 'endDate' => '2021-01-30', ), 1 => array ( 'week' => '2', 'startDate' => '2021-01-10', 'endDate' => '2021-01-16', ), 2 => array ( 'week' => '3', 'startDate' => '2021-01-17', 'endDate' => '2021-01-23', ), 3 => array ( 'week' => '1', 'startDate' => '2021-01-03', 'endDate' => '2021-01-09', ), 4 => array ( 'week' => '4', 'startDate' => '2021-01-24', 'endDate' => '2021-01-30', ), )
I have a table with sales. From this table i take all results of the last 30 days, sum the prices with the same date and get this as array.
SQL:
SELECT date
, price
, id
, SUM(price) AS daylieprice
FROM sales
WHERE id = :id
AND date BETWEEN DATE_FORMAT(CURDATE() , '%Y-%m-%d') - interval 1 month AND DATE_FORMAT(CURDATE() , '%Y-%m-%d'))
GROUP
BY date
So i have for example:
ARRAY ['date'] - ARRAY ['daylieprice']
"2017-03-29" - "1"
"2017-04-02" - "5"
"2017-04-04" - "3"
Google chart is looking like that:
['<? echo date('d', strtotime("-2 day")) ?>', VALUE]
['<? echo date('d', strtotime("-1 day")) ?>', VALUE]
['<? echo date('d') ?> ', VALUE]
Is there a way to output the value of the array like that:
date('d', strtotime("-2 day") , ARRAY ['daylieprice']);
date('d', strtotime("-1 day") , ARRAY ['daylieprice']);
date('d', ARRAY ['daylieprice']);
Should mean to take the array value easy with date('d') or date('d', strtotime("-1 day") witouth making a loop for each value ?
Or does i have to make for every day a sql request?
I came up with this. I use DateTime to give more control and felxibility with input and output formats. This loops through your input array and subtracts 2 days from first entry, 1 day from 2nd entry and keeps 3rd entry the same:
<?php
$input = [
[
'date' => '2017-03-29',
'daylieprice' => 1,
],
[
'date' => '2017-04-02',
'daylieprice' => 5,
],
[
'date' => '2017-04-04',
'daylieprice' => 3,
],
];
$output = [];
$number_of_dates = count($input) - 1;
foreach ($input as $v) {
$date = DateTime::createFromFormat('Y-m-d', $v['date'])
->modify(sprintf('-%d days', $number_of_dates))
->format('Y-m-d');
$number_of_dates--;
$output[] = "'" . $date . "', " . $v['daylieprice'];
}
This produces an array like:
Array
(
[0] => '2017-03-27', 1
[1] => '2017-04-01', 5
[2] => '2017-04-04', 3
)
Hope this helps and you can figure out exactly how to implement it to solve your problem.
Edit: just saw echo date('d' so maybe you only want the day of the month, that's easy, you can just change ->format('Y-m-d'); in the loop to ->format('d');
Demo: https://eval.in/784353
We all know date('w').
What I am trying to do is to find the next Date with the index retrieved from another date('w').
I tried:
$saturday = 6;
if((int)date('w') < $saturday){
$targetdate = strtotime('last Sunday +'.$saturday.' days');
}else{
$targetdate = strtotime('next Sunday +'.$saturday.' days');
}
But it ist not as reliable as I hoped when today is sunday.
Any best practice on this?
I could not find a quick way to do that. But I found a passable way around it (in case it helps someone):
$days = array(
0 => 'Sunday',
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday'
);
$targetdate = strtotime('next '.$saturday]);
Please just do:
date("l", strtotime("monday +$weekday_index days" ) )
Smoother than having to define an array each time!
I have a contact table with date field for birthdate.
Now I would like to show list of birthday within next week. I can show it, but the order is based on the year.
How can I order the data, just based on month and day only?
$sideBarTaskSuggestion =
$this->Project->Contact->find('all', array(
'conditions' => array(
'Contact.group_id' => $this->Session->read('Auth.User.group_id'),
'Contact.birthdate NOT' => null,
'AND' => array(
array('Contact.birthdate NOT' => null),
array('Contact.birthdate + INTERVAL EXTRACT(YEAR FROM NOW()) -
EXTRACT(YEAR FROM Contact.birthdate) YEAR <=' => date('Y-m-d',
strtotime('+1 week'))),
array('Contact.birthdate + INTERVAL EXTRACT(YEAR FROM NOW()) -
EXTRACT(YEAR FROM Contact.birthdate) YEAR >=' => date('Y-m-d')),
)
),
'order' => 'Contact.birthdate DESC'
)
);
When you say you want 'order the data, just based on month and day only?', I assume you mean:
Nov 22, 2013
Oct 21, 1922
Mar 5, 2000
Jan 31, 2001
Jan 1, 1990
Then try this:
'order' => 'DAYOFYEAR(Contact.birthdate) DESC'
I am building a small class combination to calculate the precise date of the beginning of a semester. The rules for determining the beginning of the semester goes as follow :
The monday of week number ## and after dd-mm-yyyy date
ie: for winter its week number 2 and it must be after the january 8th of that year
I am building a resource class that contain these data for all the semesters (4 in total). But now I am facing an issue based on the public holidays. Since some of those might be on a Monday, in those cases I need to get the date of the Tuesday.
The issue I am currently working on is the following :
The target semester begins on or after august 30 and must be on week 35.
I also have to take account of a public holiday which happen on the first monday of september.
The condition in PHP terms is the following
if (date('m', myDate) == 9 // if the month is september
&& date('w', myDate) == 1 // if the day of the week is monday
&& date('d', myDate) < 7 // if we are in the first 7 days of september
)
What would be the best way to "word" this as a condition and store it in an array?
EDIT
I might not have been clear enough, finding the date is not the problem here. The actual problem is storing a condition in a configuration array that looks like the following :
$_ressources = array(
1 => array(
'dateMin' => '08-01-%',
'weekNumber' => 2,
'name' => 'Winter',
'conditions' => array()
),
2 => array(
'dateMin' => '30-04-%',
'weekNumber' => 18,
'name' => 'Spring',
'conditions' => array()
),
3 => array(
'dateMin' => '02-07-%',
'weekNumber' => 27,
'name' => 'Summer',
'conditions' => array()
),
4 => array(
'dateMin' => '30-08-%',
'weekNumber' => 35,
'name' => 'Autumn',
'conditions' => array("date('m', %date%) == 9 && date('w', %date%) == 1 && date('d', %date%) < 7")
)
);
The issue I have with the way it's presented now, is that I will have to use the eval() function, which I would rather not to.
You said:
The target semester begins on or after august 30 and must be on week 35.
If that's the case you can simple check for week number.
if(date('W', myDate) == 35)
Or if your testing condition is correct then you should compare day number till 7 as it starts from 1.
if((date('m', myDate) == 9 // september
&& date('w', myDate) == 1 // monday
&& date('d', myDate) <= 7 // first 7 days of september
)
And then in the if statement, once you have found the monday which would be OK IF its not a public holiday, do this
if(...){
while(!array_search (myDate, aray_of_public_holidays))
date_add($myDate, date_interval_create_from_date_string('1 days'));
}
Here the array_of_public_holidays contains the list of public holidays.
Update with Code
Following code should work for your purposes
<?php
// array with public holidays
$public_holidays = array(/* public holidays */);
// start on 30th august
$myDate = new DateTime('August 30');
// loop till week number does not cross 35
while($myDate->format('W') <= 35){
// if its a monday
if($myDate->format('w') == 1){
// find the next date not a public holiday
while(array_search($myDate, $public_holidays))
$myDate->add(date_interval_create_from_date_string('1 days'));
// now myDate stores the valid semester start date so exit loop
break;
}
// next date
$myDate->add(date_interval_create_from_date_string('1 days'));
}
// now myDate is the semester start date
?>
Update according to updated question
Following code should work for your needs. You do not need to store the condition in your array as PHP code. The following code shows how it can be done
// semester conditions
$sem_conditions = array(
1 => array(
'dateMin' => '08-01-%',
'weekNumber' => 2,
'name' => 'Winter'
),
2 => array(
'dateMin' => '30-04-%',
'weekNumber' => 18,
'name' => 'Spring'
),
3 => array(
'dateMin' => '02-07-%',
'weekNumber' => 27,
'name' => 'Summer'
),
4 => array(
'dateMin' => '30-08-%',
'weekNumber' => 35,
'name' => 'Autumn'
)
);
// array with public holidays format (d-M)
$public_holidays = array('05-09', '10-01');
// store sem starts
$sem_starts = array();
// for each semester
foreach($sem_conditions as $sem){
// start date
$myDate = date_create_from_format('d-m', substr($sem['dateMin'], 0, -2));
// loop till week number does not cross $sem['weekNumber']
while($myDate->format('W') <= $sem['weekNumber']){
// if its a monday
if($myDate->format('w') == 1){
// find the next date not a public holiday
while(array_search($myDate->format('d-m'), $public_holidays) !== false)
$myDate->add(date_interval_create_from_date_string('1 days'));
// now myDate stores the valid semester start date so exit loop
break;
}
// next date
$myDate->add(date_interval_create_from_date_string('1 days'));
}
// add to sem starts
$sem_start[$sem['name']] = $myDate->format('d-m-Y');
}
var_dump($sem_start);
The target semester begins on or after august 30 and must be on week 35
The start of the semester is the minimal date between week 35 and August 30:
$week35 = new DateTime("January 1 + 35 weeks");
$august30 = new DateTime("August 30");
$start = min($week35, $august30);
Alternatively:
$start = min(date_create("January 1 + 52 weeks"), date_create("August 30"));