Suppose I have a date available with me: 2016-07
I want to find date range of Week like this:
2016-07-03 to 2016-07-09
2016-07-10 to 2016-07-16
2016-07-17 to 2016-07-23
2016-07-24 to 2016-07-30
How can i achieve this?
I have tried using MySql but failed. Now I'm trying to do php.
This piece of code should do exactly what you want, if not please tell me or if you have questions
$date = "2016-07"; //Your date, can be any given date of this format
$monthAndYear = explode("-", $date);
$weeks = array();
$start = mktime(0, 0, 0, $monthAndYear[1], 1, $monthAndYear[0]);
$start -= 86400; //There are surely better ways
//find beginning if first week
do
{
$start += 86400;// Add one day
$tmp = date("N", $start);
}
while($tmp != 1);
//Get all weeks. If you want only those which are in the current month use this instead:
//while(date("n", ($start + 6 * 86400)) == $monthAndYear[1])
while(date("n", $start) == $monthAndYear[1])
{
$date = date("Y-m-d", $start);
$start += (6 * 86400);
$date .= " to ".date("Y-m-d", $start);
$weeks[] = $date;
$start += 86400;
}
echo "<pre>";
var_dump($weeks);
echo "</pre>";
The output is this:
array(4) {
[0]=>
string(24) "2016-07-04 to 2016-07-10"
[1]=>
string(24) "2016-07-11 to 2016-07-17"
[2]=>
string(24) "2016-07-18 to 2016-07-24"
[3]=>
string(24) "2016-07-25 to 2016-07-31"
}
With the second while option the result will be the same because the 31th is the end of the month and also the end of a week.
Please try the following code it produces the same output as yours:
function weeksRangesOfMonth($month, $year) {
$start_time = mktime(0, 0, 0, $month, 1, $year);
$start_time = mktime(0, 0, 0, $month, date('d', strtotime('First Monday', $start_time)), $year);
$end_time = mktime(0, 0, 0, $month, date('t', $start_time), $year);
$start = date("Y-m-d", $start_time);
$end = date("Y-m-d", $end_time);
for($date = $start; $date <= $end; $date = date('Y-m-d', strtotime($date. ' + 7 days'))) {
$time= strtotime($date);
$from = date("Y-m-d", strtotime('Last Sunday', $time));
$to = date("Y-m-d", strtotime('Next Saturday', $time));
echo "Start: ".$from.", End: ".$to;
echo "<br>";
}
}
weeksRangesOfMonth(7, 16);
Demo: http://codepad.org/mWSxphvA
Related
How do I compute the start and end date of a week by month, year, and week number? The year is a 4-digit integer, such as 2016; the month is an integer, range 1-12; the week is an integer in the range 1-5.
I already have this code block:
function getStartAndEndDate($week, $year)
{
$time = strtotime("1 $year", time());
$day = date('w', $time);
$time += ((7 * $week) + 1 - $day) * 24 * 3600;
$return[0] = date('Y-n-j', $time);
$time += 6 * 24 * 3600;
$return[1] = date('Y-n-j', $time);
return $return;
}
How can I use or rewrite this function?
As for your function above in the question I have no idea, but assuming the week starts on Monday and ends on Sunday something like this might work
function getFirstandLastDate($year, $month, $week) {
$thisWeek = 1;
for($i = 1; $i < $week; $i++) {
$thisWeek = $thisWeek + 7;
}
$currentDay = date('Y-m-d',mktime(0,0,0,$month,$thisWeek,$year));
$monday = strtotime('monday this week', strtotime($currentDay));
$sunday = strtotime('sunday this week', strtotime($currentDay));
$weekStart = date('d M y', $monday);
$weekEnd = date('d M y', $sunday);
return $weekStart . ' - ' . $weekEnd;
}
echo getFirstandLastDate( 2016, 1, 1 );
I am trying to get third previous month from inputted month-year ie. if I give 06-2016 as an input, it should give me 03-2016 as result. I have tried strtotime("-3 Months"). But it gives me just 3 months from current date time.
Can someone tell me how to solve it please.
I have tried:
$month = 06;
$year = 2016;
$d = mktime(0, 0, 0, $month, 1, $year);
$td = date("Y-m-d", $d);
$oldDate = date('Y-m-d', strtotime("-3 Months"));
It gives me 3 months from current date. Any guesses how do I get the expected result ?
Use below. You need to use date("F Y", strtotime( INPUT_DATE ." -3 months"))
$month = 06;
$year = 2016;
$d = mktime(0, 0, 0, $month, 1, $year);
$td = date("Y-m-d", $d);
echo $olddate = date("Y-m-d", strtotime( $td ." -3 months"));
echo $months = date("F Y", strtotime( $td ." -3 months"));
Output:
2016-03-01
March 2016
Online Demo: Click Here
strtotime of (Input Date ."-3 Months")
$month = 06;
$year = 2016;
$d = mktime(0, 0, 0, $month, 1, $year);
$td = date("Y-m-d", $d);
$oldDate = date('Y-m-d', strtotime($td."-3 Months"));
$oldDate_MonthYear = date('m-Y', strtotime($td."-3 Months"));
echo $oldDate;
echo '<br />';
echo $oldDate_MonthYear;
Use Object Based.
$date1 = new DateTime("2016-06-01");
echo $startDate = $date1->modify("-3 month")->format('Y-m-d');
Try this and you should get desired result.
$prev_month_ts = strtotime('2016-06 -3 month');
$prev_month = date('Y-m', $prev_month_ts);
echo $prev_month;
I am writing a program code to display the week-number between two dates. I als0 want to display the dates between the week-number. I have written the following code as per the example codes from stack overflow.(here && here)
function getStartAndEndDate($week, $year)
{
$time = strtotime("1 January $year", time());
$day = date('w', $time);
$time += ((7*$week)+1-$day)*24*3600;
$return[0] = date('dMY', $time);
$time += 6*24*3600;
$return[1] = date('dMY', $time);
return $return;
}
$startTime = strtotime('2013-04-01');
$endTime = strtotime('2013-05-03');
$weeks = array();
while ($startTime < $endTime)
{
$weeks[] = date('W', $startTime);
$startTime += strtotime('+1 week', 0);
}
echo count($weeks)."<br/>";
$year="2013";
foreach ($weeks as $key => $w)
{
echo "Week Number:".$w."--";
$return=getStartAndEndDate($w,$year);
$r0=$return[0];
$r1=$return[1];
echo "Start-".$r0."-End-".$r1."<br/>";
}
But the output is wrong. Output shows:
5
Week Number:14--Start-07Apr2013-End-13Apr2013
Week Number:14--Start-07Apr2013-End-13Apr2013
Week Number:15--Start-14Apr2013-End-20Apr2013
Week Number:16--Start-21Apr2013-End-27Apr2013
Week Number:17--Start-28Apr2013-End-04May2013
14th week starts from 31Mar2013 to 6Apr2013. I couldn't figure out what the problem is. Any help must be appreciated!
Did you try to check with date
$startTime = strtotime('2012-12-30');
$endTime = strtotime('2013-05-03');
The actual14th week is
Week Number:14--Start-08Apr2013-End-14Apr2013
not
14th week starts from 31Mar2013 to 6Apr2013
The PHP start weeks from Monday -->Sunday. So if you take 2012-12-30 which is Sunday it will calculate
Week Number:01--Start-07Jan2013-End-13Jan2013
Week Number:02--Start-14Jan2013-End-20Jan2013
Week Number:03--Start-21Jan2013-End-27Jan2013
Week Number:04--Start-28Jan2013-End-03Feb2013
Week Number:05--Start-04Feb2013-End-10Feb2013
Week Number:06--Start-11Feb2013-End-17Feb2013
Week Number:07--Start-18Feb2013-End-24Feb2013
Week Number:08--Start-25Feb2013-End-03Mar2013
Week Number:09--Start-04Mar2013-End-10Mar2013
Week Number:10--Start-11Mar2013-End-17Mar2013
Week Number:11--Start-18Mar2013-End-24Mar2013
Week Number:12--Start-25Mar2013-End-31Mar2013
Week Number:13--Start-01Apr2013-End-07Apr2013
Week Number:14--Start-08Apr2013-End-14Apr2013
I have figured it out:
function week_start_date($wk_num, $yr, $first = 1, $format = 'F d, Y')
{
$wk_ts = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101'));
$mon_ts = strtotime('-' . date('w', $wk_ts) + $first . ' days', $wk_ts);
return date($format, $mon_ts);
}
$year="2013";
$startDate="2012-12-30";
$endDate="2013-05-03";
$p = new DatePeriod(
new DateTime($startDate),
new DateInterval('P1W'),
new DateTime($endDate)
);
foreach ($p as $k=>$w) {
echo $k.":".$w->format('W')."<br/>";
$w=$w->format('W');
$sStartDate = week_start_date($w, $year);
$sEndDate = date('F d, Y', strtotime('+6 days', strtotime($sStartDate)));
echo $sStartDate."&&".$sEndDate."<br/>";
}
I'm having an issue with my code not displaying the correct difference between dates. The Days Minutes and Seconds all work correctly but the Hours seem to be displayed the subtracted amount and not the remainder if that makes sense at all.
For example, using these dates
2171167 = 2013-05-18 00:00:00 - 2013-04-22 20:53:53
I receive the following output
25 days 19:06:07
$date_one = date('Y-m-d H:i:s');
$date_two = date('Y-m-d H:i:s', mktime(0, 0, 0, 5, 18, 2013));
$Difference = abs(strtotime($date_two) - strtotime($date_one));
$Days = date("d", $Difference);
//$Hours = date("H", $Difference); Why does this NOT WORK???
$Minutes = date("i", $Difference);
$Seconds = date("s", $Difference);
If you could please tell me why the second "Hours" variable i have commented out is not working i would very much appreciate it.
<?php
header('Content-Type: text/plain');
$date1 = DateTime::createFromFormat('Y-m-d H:i:s', '2013-05-18 00:00:00');
$date2 = DateTime::createFromFormat('Y-m-d H:i:s', '2013-04-22 20:53:53');
$result = $date1->diff($date2);
echo $result->format('%Y-%m-%d %H:%i:%s');
?>
Shows:
00-0-25 03:6:7
To split into variables:
list($year, $month, $day, $hour, $minute, $second) = explode('-', $result->format('%Y-%m-%d-%H-%i-%s'));
var_dump($year, $month, $day, $hour, $minute, $second);
Shows:
string(2) "00"
string(1) "0"
string(2) "25"
string(2) "03"
string(1) "6"
string(1) "7"
you are using wrong first date use below code to your actual answer
$date_one = "2013-04-22 20:53:53"; //date('Y-m-d H:i:s');
$date_two = date('Y-m-d H:i:s', mktime(0, 0, 0, 5, 18, 2013));
$Difference = abs(strtotime($date_two) - strtotime($date_one));
echo "<br> dif ->".date('d H:i:s',$Difference);
echo "<br> day -> ".$Days = date("d", $Difference);
echo "<br> Hours -> ".$Hours = date("H", $Difference);
echo "<br> Minutes -> ".$Minutes = date("i", $Difference);
echo "<br> Seconds -> ".$Seconds = date("s", $Difference);
:OUTPUT:
dif -> 26 03:06:07
day -> 26
Hours -> 03
Minutes -> 06
Seconds -> 07
Just change the hours syntax.
<?php
$date_one = date('Y-m-d H:i:s');
$date_two = date('Y-m-d H:i:s', mktime(0, 0, 0, 5, 18, 2013));
$Difference = round(strtotime($date_two) - strtotime($date_one));
$Days = date("d", $Difference);
$Hours = date("H", $Difference);
echo $Hours = $Difference / 60;
$Minutes = date("i", $Difference);
$Seconds = date("s", $Difference);
?>
I need to get the first and last day of a month in the format YYYY-MM-DD given only the month and year. Is there a good, easy way to do this?
$first = date('Y-m-d', mktime(0, 0, 0, $month, 1, $year));
$last = date('Y-m-t', mktime(0, 0, 0, $month, 1, $year));
See date() in PHP documentation.
First day is always YYYY-MM-01, isn't it? Example: date("Y-M-d", mktime(0, 0, 0, 8, 1, 2008))
Last day is the previous day of the next month's first day:
$date = new DateTime("2008-09-01");
$date->modify("-1 day");
echo $date->format("Y-m-d");
The first day of the month is always 1.
So it will become
YYYY-MM-01
the last day can be calculated as:
<?php
$num = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31
echo "There was $num days in August 2003";
?>
OK, first is dead easy.
date ('Y-m-d', mktime(0,0,0,MM,01,YYYY));
Last is a little trickier, but not much.
date ('Y-m-d', mktime(0,0,0,MM + 1,-1,YYYY));
If I remember my PHP date stuff correctly...
**edit - Gah! Beaten to it about a million times...
Edit by Pat:
Last day should have been
date ('Y-m-d', mktime(0,0,0,$MM + 1,0,$YYYY)); // Day zero instead of -1
<?php
echo "Month Start - " . $monthStart = date("Y-m-1") . "<br/>";
$num = cal_days_in_month(CAL_GREGORIAN, date("m"), date("Y"));
echo "Monthe End - " . $monthEnd = date("Y-m-".$num);
?>
The easiest way to do this with PHP is
$dateBegin = strtotime("first day of last month");
$dateEnd = strtotime("last day of last month");
echo date("MYDATEFORMAT", $dateBegin);
echo "<br>";
echo date("MYDATEFORMAT", $dateEnd);
Or the last week
if (date('N', time()) == 7) {
$dateBegin = strtotime("-2 weeks Monday");
$dateEnd = strtotime("last Sunday");
} else {
$dateBegin = strtotime("Monday last week");
$dateEnd = strtotime("Sunday last week");
}
Or the last year
$dateBegin = strtotime("1/1 last year");
$dateEnd = strtotime("12/31 this year");
By the way #ZombieSheep solution
date ('Y-m-d', mktime(0,0,0,$MM + 1,-1,$YYYY));
does not work it should be
date ('Y-m-d', mktime(0,0,0,$MM + 1,0,$YYYY)); // Day zero instead of -1
Of course #Michał Słaby's accepted solution is the simplest.
Just to verify that I didn't miss any loose ends:
$startDay = 1;
if (date("m") == 1) {
$startMonth = 12;
$startYear = date("Y") - 1;
$endMonth = 12;
$endYear = date("Y") - 1;
}
else {
$startMonth = date("m") - 1;
$startYear = date("Y");
$endMonth = date("m") - 1;
$endYear = date("Y");
}
$endDay = date("d") - 1;
$startDate = date('Y-m-d', mktime(0, 0, 0, $startMonth , $startDay, $startYear));
$endDate = date('Y-m-d', mktime(0, 0, 0, $endMonth, $endDay, $endYear));
try this to get the number of days in the month:
$numdays = date('t', mktime(0, 0, 0, $m, 1, $Y));
Example; I want to get first day and last day of current month.
$month = (int) date('F');
$year = (int) date('Y');
date('Y-m-d', mktime(0, 0, 0, $month + 1, 1, $year)); //first
date('Y-m-d', mktime(0, 0, 0, $month + 2, 0, $year)); //last
When you run this for instance at date 2015-01-09, the first and last values will be sequentially;
2015-01-01
2015-01-31
Tested.
From here(get next month last day) that is marked as duplicated, so i can't add comment there, but people can got bad answers from there.
Correct one for last day of next month:
echo ((new DateTime(date('Y-m').'-01'))->modify('+1 month')->format('Y-m-t'));
Correct one for first day of next month:
echo ((new DateTime(date('Y-m').'-01'))->modify('+1 month')->format('Y-m-01'));
Code like this will be providing March from January, so that's not what could be expected.
echo ((new DateTime())->modify('+1 month')->format('Y-m-t'));
proper way to build a relative date from now is:
//bad example - will be broken when generated at 30 of December (broken February)
echo date("Y-m-d", strtotime("now"))."\n";
echo date("Y-m-d", strtotime("now + 1 month"))."\n";
echo date("Y-m-d", strtotime("now + 2 month"))."\n";
echo date("Y-m-d", strtotime("now + 3 month"))."\n";
//good example, you can change first day to last day or any day
echo date("Y-m-d", strtotime("first day of this month"))."\n";
echo date("Y-m-d", strtotime("first day of next month"))."\n";
echo date("Y-m-d", strtotime("first day of +2 month"))."\n";
echo date("Y-m-d", strtotime("first day of +3 month"))."\n";
and the result will be:
2021-12-30
2022-01-30
2022-03-02
2022-03-30
2021-12-01
2022-01-01
2022-02-01
2022-03-01