Comment is the output I'm seeing. Help!
echo date("Y-m-t", strtotime("2012-07-31 -1 month")); //2012-07-31
echo date("Y-m-t", strtotime("2012-07-31 -2 month")); //2012-05-31
July has 31 days, so it can't be processing it as August 1, right? Even if it were, the second line should work. Right?!
Thanks!
What's happening here is, "7-31" minus one month comes out as "6-31", which translates to "7-1", which - asking for the last day of the month - comes out as "7-31".
When you want to do month math, it's almost always, if not always, better to build the time using mktime.
$month = 7;
echo date("Y-m-t", mktime(0, 0, 0, $month - 1, 1, 2012)); //2012-06-30
Note that when doing month math in mktime, always give '1' as the day. Or really anything as long as it's 28 or lower.
You can do something like this:
$month = date('m') ;
$day = date('d') ;
$year = date('Y') ;
// ----
$current = mktime( 0, 0, 0, $month , $day, $year);
$yesterday = mktime( 0, 0, 0, $month , $day-1, $year);
$tomorrow = mktime( 0, 0, 0, $month , $day+1, $year);
// ----
echo '<pre>Current Day: '.date('Y-m-d', $current).'</pre>';
echo '<pre>Day Before: '.date('Y-m-d', $yesterday).'</pre>';
echo '<pre>Day After: '.date('Y-m-d', $tomorrow).'</pre>';
If you're looking for the last day of July:
$july = mktime( 0, 0, 0, 8, 1-1, 2012);
// Last Day of July: 2012-07-31
echo '<pre>Last Day of July: '.date('Y-m-d', $july).'</pre>';
Using mktime, just enter the first day of August and subtract one day.
Related
I have a problem with find the current date from past mktime. In PHP I find the current date using date("j");. Here I need, suppose my date was in the past year like mktime(0, 0, 0, 2, 1, 2008), then here how can I find the current date of this particular past month.
Either as #octern's solution, or you can do
$day = date('j', strtotime("-2 months"));
or
$day = date('j', strtotime('-30 days'));
depending on your need.
You may also want to refer to strtotime() manual.
Try this:
$date = getdate(mktime(0, 0, 0, 2, 1, 2008));
$day = $date['mday'];
Or just:
$day = date('j', mktime(0, 0, 0, 2, 1, 2008))
I'm wondering if there's an easy way (like using strtotime) whereby I can get the unix time for the last occurrence of a day/month combination. For example, if I was to ask for "1st of September" today (9th May 2012) I would get 1314835200 (1st Sep 2011), but if the code was to run again this October, I would get 1346457600 (1st Sep 2012), and the same if I ran it 1 year from now.
Being able to do it forwards as well as backwards would be a massive bonus.
$month = 9;
$day = 1;
$timestamp = mktime(0, 0, 0, $month, $day);
if ($timestamp > time()) {
$timestamp = mktime(0, 0, 0, $month, $day, date('Y') - 1);
}
How can I get last year's start and end date using PHP code? Is it possible?
The first day is always January 1, the last day is always December 31. You're really only changing the year attached to it. Depending on how you want the date formatted, you have a couple possibilities...
If you just want to display the physical date:
$year = date('Y') - 1; // Get current year and subtract 1
$start = "January 1st, {$year}";
$end = "December 31st, {$year}";
If you need the timestamp for both those dates:
$year = date('Y') - 1; // Get current year and subtract 1
$start = mktime(0, 0, 0, 1, 1, $year);
$end = mktime(0, 0, 0, 12, 31, $year);
Very simple stuff. You can manually specify which year if you wanted too. The premise is the same.
You can do it by using the below. Hope it helps someone.
//to get start date of previous year
echo date("d-m-y",strtotime("last year January 1st"));
//to get end date of previous year
echo date("d-m-y",strtotime("last year December 31st"));
start date of the year :
mktime(0,0,0,1,1,$year);
end date of the year :
mktime(0,0,0,1,0,$year+1);
Check this Stuff
$currentY = date('Y');
$lastyearS = mktime(0, 0, 0, 1, 1, $currentY-1 )."<br/>";
$lastyearE = mktime(0, 0, 0, 12, 31, $currentY-1 )."<br/>";
echo date('Y-m-d',$lastyearS)."<br/>";echo date('Y-m-d',$lastyearE);
Suppose if your current month is February or the month which has 30 days
echo date('Y-12-t', strtotime(date('Y-m-d'))); // if current month is february (2015-02-01) than it gives 2015-02-28
will give you inaccurate results
Solution:
So to get accurate result for the end date of an year, try the code below
$start_date = date("Y-01-01", strtotime("-1 year"));// get start date from here
$end_date = date("Y-12-t", strtotime($start_date));
(OR)
$last_year_last_month_date = date("Y-12-01", strtotime("-1 year"));
$end_date = date("Y-12-t", strtotime($last_year_last_month_date));
I want a First Day Of a week say I have 45th week. Now I want to have date of the first sunday of this week.
Can any one suggest me how I can go about this ?
Thank you
Searching around a bit, the most suggested is:
$year = "2009"; // date("Y");
$week = "45"; // date("W");
$firstDayOfWeek = strtotime($year."W".str_pad($week,2,"0",STR_PAD_LEFT));
print("The first day of week ".$week." of ".$year." is ".date("D, d-m-Y",$firstDayOfWeek));
Basically this comes down to letting strtotime do the work for you. You fill in "2009W45" and it'll retrieve the date for you.
Pitfall here is that the week needs to be in a 2 digit format. So week 1 needs to be 01, hence the str_pad to zero-fill it.
you should strtotime and date function
some code like that
$next_sunday = strtotime('next sunday');
$next_week = strtotime('+1 week');
$next_sunday_of_next_week = strtotime('next sunday', $next_week);
hope this helps
If you have the month, day and year:
$timestamp = mktime(0, 0, 0, $month, $day, $year);
echo date('c', $timestamp) = mktime(0, 0, 0, $month, date('d', $timestamp)-date('w', $timestamp), $year);
you could also do it this way if you have the timestamp:
echo $date('c', mktime(0, 0, 0, $month, date('d', $timestamp)-date('w', $timestamp), $year));
From: http://pinoytech.org/blog/post/get-the-first-day-of-the-week-with-an-exact-date
date($str_format, strtotime($year."W".$week."1"))
Where $str_format is the output formate according to the PHP date() function.. I use 'M d Y'
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