I want to generate a report based on this week and this month. How do I calculate this?
I mean: how do I get today's date and query the database based on the "... BETWEEN monday AND today"
date("N") will give you the day of the week ( 1-monday to 7-sunday )
So move back that number of days will take you back to Sunday.
BUT since we want Monday, N-1 will take you back to Monday instead
Monday 1 -(1-1) = 0 days
Tuesday 2 -(2-1) = 1 day
...
Sunday 7 - (7-1) = 6 days
date("Y-m-d", timestamp); will give the date
date("Y-m-d 00:00:00, timestamp) will give the datetime
date("Y-m-01", timestamp); will give the first day of the month
Use these in you query as required
e.g.
<?php
$month_start=date("Y-m-01");
$last_monday=date("Y-m-d", strtotime("-".(date("N")-1)." days"));
$today=date("Y-m-d");
$sql1="SELECT * FROM table WHERE my_date BETWEEN :month_start AND :today";
$sql2="SELECT * FROM table WHERE my_date BETWEEN :last_monday AND :today";
?>
I've omitted the sql as we dont know if your mysql_ mysqli_ or PDO
$date= new DateTime(date('Y-m-d'));
while(true)
{
if($date->format("w") != 1 )// 0 for sunday, 1 for monday, etc
{
//do things
$date->modify("-1 day");
}else{
return;
}
}
Related
I have a date 2015-12-16
i want to get the date of the first day's date for the current week of my date
here 2015-12-16 is in the week 51 of the year then i want to get the first day's date of the week 51 ( 2015-12-14 here)
how could i do it ?
thank you
EDIT: it must work when there are 53 weeks in the year (like in 2015 for example)
You can do the following:
$dateTime = new DateTime("2015-12-16");
$weekNo = $dateTime->format("W");
$newDate = new DateTime();
$newDate->setISODate($dateTime->format("Y"), $weekNo);
Example:
http://sandbox.onlinephpfunctions.com/code/281a1ac298bfee8be421e333e4b7e92c6bb44d65
Since the above is a bit off in some cases here's something more reliable:
$dateTime = new DateTime("2016-01-01");
$dateTime->sub(new DateInterval("P".($dateTime->format("w")-1)."D")); //Since the weekdays are 1-based.
Example:
http://sandbox.onlinephpfunctions.com/code/c5cb0f077fa77974d977ddbffa6bc0b61f9d7851
$date = new \DateTime('2015-12-16');
echo $date->modify('last sunday +1 day')->format('Y-m-d');
This gets start of this week if you count monday to sunday.
Try this:
date('Y-m-d', strtotime('This week', strtotime('2015-12-16')));
I have array() of week days to repeat my event.
[days_repeat] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
1,2,3 are the events should be repeated every monday, tuesday, wednesday, but I have also
[start_date] => 2014-04-2
[end_date] => 2014-05-30
As you can see start day isn't the first day to repeat, if it was then was easier, just loop and make dates and break loop, when date is bigger than end date, but maybe you can suggest me some good tactics to achieve this
Days repeat array can contain only week day numbers and so!
First, convert start_date to a timestamp.
Then using php date() function as such :
echo date('w', $your_timestamp);
You get to know what day is the day of first_date, 0 (for Sunday) through 6 (for Saturday).
Then just loop and go through your days checking if they match your array.
Try this:
$daysRepeat = array(1,2,3);
$startDate = '2014-04-02';
$endDate = '2014-05-30';
//Convert to timestamps;
$startDateTstamp = strtotime($startDate);
$endDateTstamp = strtotime($endDate);
//Loop through days between start and end date
$tstamp = $startDateTstamp;
while($tstamp < $endDateTstamp) {
//Calculate numerical representation of week day (1-7)
$weekDay = date('N', $tstamp);
if(in_array($weekDay, $daysRepeat)) {
print date('Y-m-d', $tstamp) . "<br />";
}
$tstamp += (60*60*24);
}
Output:
2014-04-02
2014-04-07
2014-04-08
2014-04-09
2014-04-14
2014-04-15
2014-04-16
2014-04-21
2014-04-22
2014-04-23
2014-04-28
2014-04-29
2014-04-30
2014-05-05
2014-05-06
2014-05-07
2014-05-12
2014-05-13
2014-05-14
2014-05-19
2014-05-20
2014-05-21
2014-05-26
2014-05-27
2014-05-28
if your data represents the days within a week (so first day of the week till the seventh day of the week), than you just need to check the current displayed day, if its the same "daynumber"
eg.
echo (new DateTime())->format("w");
will print
1
for monday
see the docs for more informations.
http://www.php.net/manual/en/function.date.php
so youll need only to check, if the current daynumber is in your repeat array!
You can achieve this with this code:
<?php
$days_repeat = array(
'1', // Monday
'2', // Tuesday
'3', // Wednesday
);
// first set timezone
date_default_timezone_set('UTC');
// Start date
$date = '2014-04-02';
// End date
$end_date = '2014-05-30';
while (strtotime($date) <= strtotime($end_date)) {
// first find out which day of the week it is (0=sunday; 1=monday ... 6=saturday)
$dateWeekDay = date("w", $date);
// convert sunday from 0 to 7 if needed
if ($dateWeekDay == 0) { $dateWeekDay = 7; }
// check if $days_repeat has current day
if (in_array($dateWeekDay, $days_repeat)) {
// do action as this day is in $days_repeat
echo date("Y-m-d"). ' is in $days_repeat';
}
// add +1 day and continue loop
$date = date("Y-m-d", strtotime("+1 day", strtotime($date)));
}
?>
Conveniently, you are representing the day of the week according to the ISO-8601 specification. This means you can use PHP's built-in date() function with no additional translation to your format. That's nice.
Next, you need to figure out how to convert your start_date and end_date into a date format PHP can understand. It looks like mktime() is your best bet here - initialize the time elements to 0 if you don't care about them.
You can now iterate by day and if date("N", $currentDay) is in your array of wanted days, repeat the event on that day (whatever that entails). Here, $currentDay is the looped timestamp you're checking. date("N") returns the same ISO-8601 format day-of-week number as specified in the manual page for date().
Hy, I have in database number of the week date ("W") and I want to display week interval like 28 Jan -> 3 Feb in this format, and I don't know if it's possible. Can you help?
Thanks!
Try this
$year = 2013;
$week_no = 6;
$week_start = new DateTime();
$week_start->setISODate($year,$week_no);
$week_end = clone $week_start;
$week_end = $week_end->add(new DateInterval("P1W"));
echo $week_start->format('d-M-Y') . " - ".$week_end->format('d-M-Y');
Transform your intervals into timestamps.
If its not the first day of the week get the first day of that week with strtotime "last sunday" (or monday) for the first date.
Do the same for the second date this time geting the last day of the week with "next saturday" (or sunday);
Get both dates W and make a mysql comparison between the weeks.
I'm aware of the vast amount of date questions on SO but I am stumped at this one.
I need to run some reports and in order to do so I need to work out some dates.
The report will run on a Monday and I need to work out the dates of the Sunday the week before and the following Saturday.
e.g. The report will be run on Monday the 28th May and the dates I need are Sunday the 20th May and Saturday the 26th May.
I then need the same values but for the previous week, so Sunday the 13th May and Saturday the 19th May.
I think this part will be fine as it's just a case of getting the current date of the Monday when the report is run and and manipulating it from there.
THEN finally, the part I can't work out is the first week I mentioned, I need the corresponding dates from last year, so the dates of the Sunday and Saturday from the same week number in the previous year.
I know the date function can give you the week number but can't see how to work out the dates of the Sunday of that week and the previous Saturday based on that.
This is what I have, forgive the OTT variable names:
$today = date('Y/m/d');
// reports run on a Monday and use last Sunday and the following Saturday's date so get yesterday's date first
$yesterday = strtotime ('-1 day', strtotime($today));
$yesterday = date ('Y/m/d', $yesterday);
// start of week (last Sunday)
$start_of_last_week = strtotime ('-1 week', strtotime($yesterday));
$start_of_last_week = date ('Y/m/d', $start_of_last_week);
// end of last week (the following Saturday)
$end_of_last_week = strtotime ('+ 6 days', strtotime($start_of_last_week));
$end_of_last_week = date ('Y/m/d', $end_of_last_week;
// start of previous week
$start_of_previous_week = strtotime ('-1 week', strtotime($start_of_last_week));
$start_of_previous_week = date ('Y/m/d', $start_of_previous_week);
// end of previous week
$end_of_previous_week = strtotime ('+ 6 days', strtotime($start_of_previous_week));
$end_of_previous_week = date ('Y/m/d', previous;
// the start of the same week last year
$start_of_last_week_last_year = strtotime ('-1 year', strtotime($start_of_last_week ));
But the above isn't right so not sure what to do next.
Any help is much appreciated.
You can find out the date of the Monday of a particular year and week number like this
date( 'Y-m-d', strtotime( '2012W21' )); //2011-05-23
Compound Date Formats
What is the first day of the week in mysql, Monday or Sunday??
I want to do this: (i am using php btw)
Get today date (no problem)
Display information of this week (stuck here)
Previous and next week button, to display previous and next week data (cant do anything here)
I am kinda of stuck while playing with the "date" thing. Can anyone help?
Monday.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_weekday
WEEKDAY(date)
Returns the weekday index for date (0 = Monday, 1 = Tuesday, … 6 = Sunday).
To get details about previous or next weeks you can use
DATE_ADD(date,INTERVAL expr unit), DATE_SUB(date,INTERVAL expr unit)
eg SELECT DATE_ADD('2010-12-31 23:59:59', INTERVAL 1 WEEK);
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add
To get the weekday in PHP you can use the following:
<?php
$datetime = new DateTime('2010-12-31 23:59:59');
$today = $datetime->format('l');
echo $today;
?>
http://www.php.net/manual/en/function.date.php