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'
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'm trying to match all the rows in a table to a specific month. I want my output to look like this:
return [
'January' => [
'total_sales' => 100
],
'February' => [
'total_sales' => 50
],
'Match' => [
'total_sales' => 2
],
];
So the month period starts at the beginning of the year (January) and ends until today's date (April) if we are in June then I should see total sales from June, if we are in July same thing etc..
This is what I have so far to loop from the beginning of the year until today's month.
for($i=date('n');$i>0;$i--)
{
var_dump(date('F', mktime(0, 0, 0, ($i), 2, date('Y'))));
}
This is what I have to pull query from mySQL:
`SELECT * FROM total_sales WHERE created_at > (NOW() - INTERVAL {starts from January till today's month} MONTH)`
My Problem:
How do can I display the output I can to accomplish above?
You can group on month,
select date_format(created_at,"%b"),sum(sale_amount) from total_sales where created_at > '2020-01-01 00:00:00' group by date_format(created_at,"%b");
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
What would be the best way to find the difference in the two dates start_date and account_add_date then insert into a column called account_age?
What I need is for a user to select a start_date say its January.
Then the date when they added account_add_date say March.
The difference is 2 months so in account_age would be 2,
then in April that 2 would be a 3. May 4, June 5 and so on
Dose anyone know how to do this ?
My current insert query MODEL
function create_bank()
{
$this->load->helper('date');
$new_bank_acc_insert_data = array(
'bank_name' => $this->input->post('bank_name'),
'interest' => ($this->input->post('interest') / 100),
'start_amount' => $this->input->post('start_amount'),
'length' => $this->input->post('length'),
'start_date' => date('Y-m-d',strtotime($this->input->post('start_date'))),
'mem_id' => $this->session->userdata('id'),
'account_add_date' => $this->current_date()
);
$insert = $this->db->insert('bank', $new_bank_acc_insert_data);
return $insert;
}
Idea on finding account age
SELECT DATEDIFF('start_date','account_add_date')
SELECT
(
12* (YEAR(account_add_date) - YEAR(start_date)) +
(MONTH(account_add_date) - MONTH(start_date))
) AS differenceInMonth
FROM
YOUR_TABLE
Although DATEDIFF function returns the difference in two dates in days which is more accurate comparing to the difference in month
Explanation:
Example:
start_date = 2014 March
account_add_date = 2015 January
YEAR(account_add_date) = 2015
YEAR(start_date) = 2014
MONTH(account_add_date) = 1
MONTH(start_date) = 3
So according to the query:
12 * (2015-2014) + (1-3) = 12 * 1 - 2 = 10 (Months)
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"));