EDITED directly to the problem :
The code :
<?php
$date = new DateTime('2000-01-31'); // or whatever
for ($i = 0; $i < 100; $i++) {
$currentDay = $date->format('d');
if ($currentDay < $date->format('t')) {
$date->modify('+1 month');
if ($date->format('d') < $currentDay) {
$date->modify('last day of previous month');
}
} else {
$date->modify('last day of next month');
}
echo $date->format('Y-m-d') . "<br>";
}
So, if starting date is 2000-01-31, it works fine. That's just because 31 is the last day of january, so, any other month it will put the last date of the month.
But, if u change starting date to 2000-01-30, it's broken. That's because 30 january is not the last day in january. But anyway, 30 january is greater than days in february, so it transform date to 28/29 february. Since 28/29 february is the last day in february, it proceed the code like when this date == number of days in the month, and on the next iteration instead of putting 30 march, it puts 'last day of next month' (31 of march).
And that's not the unique case. Same thing if u put starting date for example 30-08-2000. 30 is not the last day of august, so it change the date to 30 of september, 30 september is the last day of the september, so it change the date to 31 of octomber, but it's not what I expect.
If I've understood your question correctly then this should be what you're after:
$date = new DateTime('2000-01-31'); // or whatever
$day = $date->format('d');
$date->setDate($date->format('Y'), $date->format('m'), 1);
for ($i = 0; $i < 100; $i++) {
$date->modify('+1 month');
echo echo $date->format('t') < $day ? $date->format('Y-m-t') : $date->format('Y-m-' . $day);
echo '<br />';
}
Hope this helps!
Do I understand correctly that
you want to print the same day for all following months
in case it's higher than the maximum days in the month, last day of that month should be used instead
If yes, this could work:
<?php
$date = new DateTime('2000-01-28'); // or whatever
#echo $date->format('d')." ".$date->format('t');
$expectedDay = $date->format('d');
$month = $date->format('m');
$year = $date->format('Y');
for ($i = 0; $i < 100; $i++) {
echo $date->format('Y-m-d') . "<br>";
if ($month++ == 12) {
$year++;
$month = 1;
}
$date->modify("${year}-${month}-1");
if ($expectedDay > $date->format('t')) {
$day = $date->format('t');
} else {
$day = $expectedDay;
}
$date->modify("${year}-${month}-${day}");
}
Related
Given date is 2019-12-30, and I want to add 1 month for every iteration in the loop (so 5 times).
I wrote this code:
$begin_date = '2019-12-30';
for ($i = 1; $i <= 5; $i++) {
echo '<p>' . $begin_date . '</p>';
$time = strtotime($begin_date);
$begin_date = date("Y-m-d", strtotime("+1 month", $time));
}
And I got the following result
2019-12-30
2019-01-30
2019-03-01 invalid -- I would expect 2019-02-28, at least last day
2019-04-01 invalid
2019-05-01 invalid
You can use DateTime objects, then modify the object to the last day of the next month. If the day of the month is greater than 30, subtract one (as it can never be higher than 31 anyways).
$begin_date = '2019-12-30';
$date = new DateTime($begin_date);
for ($i = 1; $i <= 5; $i++) {
echo '<p>'.$date->format("Y-m-d")."</p>\n";
$date->modify("last day of next month");
if ($date->format("d") > 30) {
$date->modify("-1 day");
}
}
Live demo at https://3v4l.org/jhX95
If you're looking for a more dynamic way (in case the date is not always the 30th), you can duplicate the original object, and subtract the current day of month from the original day of the month.
$begin_date = '2019-12-30';
$date = new DateTime($begin_date);
$original_date = clone $date;
for ($i = 1; $i <= 5; $i++) {
echo '<p>'.$date->format("Y-m-d")."</p>\n";
$date->modify("last day of next month");
if ($date->format("d") > $original_date->format("d")) {
$date->modify("-".($date->format("d") - $original_date->format("d"))." day");
}
}
Live demo at https://3v4l.org/kkOkM
i want to get each month last date for every year. year will change and it should take from currdate.
i tried below one
$day = date( 't-m-Y' );
But am writing different if condition for each month. So in this code i want to give constant month. year will change. Is it possible to do like that?
can anyone help me to do this.
Check demo here.
<?php
$year = date("Y");
echo $year . "\n";
for ($i = 1; $i <= 12; $i++)
{
$month = strtotime("$year-$i");
echo date("t", $month) . "\n";
}
output:
2017
31
28
31
30
31
30
31
31
30
31
30
31
// get Current Date
$date = date('j-m-Y');
echo date("j-m-y", strtotime($date));//Current Month date with Current year
echo "Current Year =".date("Y", strtotime($date))." , Current Month Days =".date("t", strtotime($date))." , Current Month Last Day Date =".date("t-m-Y", strtotime($date));
t returns the number of days in the month of a given date
so you can pass the dynamic year and constant month like this to know the last day of month
<?php
$year ="2017";
echo date("Y-m-t", strtotime(date("$year-04-d")));
?>
OUTPUT:
2017-04-30
update 1:
$date = new DateTime('last day of 2017-04');
echo $date->format('Y-m-d');
you can check this code
$a_date = "2017-04-17";
echo date("Y-m-t", strtotime($a_date));
or use
$current_date = "2017-04-17";
$date = new DateTime($current_date);
$date->modify('last day of this month');
echo $date->format('Y-m-d');
echo $first_date = date('Y-m-d',strtotime('first day of this month'));
echo $last_date = date('Y-m-d',strtotime('last day of this month'));
this is your code
echo $last_date = date('t-03-Y');
and this is output
30-03-2017
The last date of the month stays the same year in and year out, i.e. December will have 31 days whether referencing this year or the next. The only thing that changes is on what day of the week the last day of a month falls. So, the following indicates how to obtain all this data for the current month and the remaining months of the year as well as for next year.
<?php
// preliminaries
try {
$date = new DateTime("#".time());
} catch (Exception $e) {
echo $e->getMessage();
exit(1);
}
$strDate = $date->format("Y-n-t");
list($Y,$m,$t) = explode("-",$strDate);
$offset = 12 - $m;
$max = $offset + 1; // include current month
//this year starting with current month:
for ( $i=0; $i < $max; $i++ ){
$mo_day_one_yr = "$Y-" . ( $m + $i ) . "-1";
$date->modify( $mo_day_one_yr );
echo $date->format("D, t-M-Y"),"\n";
if( $i == ( $max -1 )) echo "\n";
}
// next year:
for ( $i=1, $Y+=1, $max=12; $i <= $max; $i++ ){
$mo_day_one_yr = "$Y-$i-1";
$date->modify( $mo_day_one_yr );
echo $date->format("D, t-M-Y"),"\n";
}
Live code
Note, the DateTime object gets initialized with a Unix timestamp string but one could also just simply provide a parameter of "now". Also, the try-catch is a good idea just in case an issue should arise in terms of instantiating the DateTime object.
I am trying to figure out on how to set my dates to every 15th and end of month only...what i'm getting so far is only +15 days from my current date..
current date = date today.LOGIC: if the current day is less than 15 then the start date of the loop is on the end of the month then if the current day is equal or greater than 15 then the start date of the loop is on the 15th. so in my case my current date is Nov 9 so the First output should be Nov 30.
$y = 1;
while ($y <= $num_term) { // num_term is equal to the number of output
$month_line = strtotime("15 day", strtotime("$month_sched"));
$day = date("d", $month_line);
$month_int = date("M-d", $month_line);
}
Output:
Nov 24`<br />`
Dec 9`<br />`
Dec 24`<br />`
Could anybody help me... thanks :)
Try this
Find the first day of month
Find the Last Day of month as Lalji Nakum told
Check your date with 15th Day of month Like example bellow
if($today < $hDay){
echo 'Start : '. date("t-m-Y");
}else if ($today >= $hDay){
echo 'Start :'. $hDay;
}
Here,
1. $today will the current date,
2. $hDay will be the 15th day of the month like 15 Nov
Here is the sample code
echo 'First day of month '.
$fDay = date('01-m-Y');
echo '<br> 15th day of month '.
$hDay = date('d-m-Y', (strtotime($fDay)+ (86400 * 15)));
echo '<br> Last Day of month '.
$lDay = date("t-m-Y");
echo '<br> Current day '.
$today = date('d-m-Y');
//$today = date('d-m-Y', strtotime($hDay)+86400 );
echo '<br>';
if($today < $hDay){
echo 'Start : '. $lDay = date("t-m-Y");
}else if ($today >= $hDay){
echo 'Start :'. $hDay;
}
You can get total number of days in month by
<?php
$a_date = "2015-11-09";
echo totaldays = date("t", strtotime($a_date));
?>
You can get current day from current date :
<?php
echo curday = date('d');
if(curday==totaldays){
echo "lastday";
}
if(curday==15)
{
echo "15th day";
}
?>
find 15th: strtotime($month_sched." +14 day");
find last day of month: strtotime($month_sched." next month - 1 hour");
Please try the following code:
<?php
$y=1;
$num_term = 10;
//start date
$month_sched = date("2012-02-01");
while($y <= $num_term) {
//15th
$month_line_15 = strtotime($month_sched." +14 day");
//last day of month
$month_line_last = strtotime($month_sched." next month - 1 hour");
$day = date("M-d", $month_line_15);
$month_int = date("M-d", $month_line_last);
$month_sched = date("Y-m-d",strtotime($month_sched." +1month"));
$y++;
}
Tested.
PHPFiddle
You can use mktime with 0 and 15 as value for the day:
$a = date("m-d", mktime(0,0,0,$month,0));
$b = date("m-d", mktime(0,0,0,$month,15));
This will give you the last day of the previous month and the 15. of $month
More than enough answers possible for this one. I would like to contribute an answer as well.
$beginDate = new DateTime('15 January');
// clone start date
$endDate = clone $beginDate;
// Add 1 year to start date
$endDate->modify('+1 year');
// Increase with an interval of one month
$dateInterval = new DateInterval('P1M');
$dateRange = new DatePeriod($beginDate, $dateInterval, $endDate);
foreach ($dateRange as $day) {
echo $day->format('Y-m-d')."<br />"; // 15th
echo $day->format('Y-m-t')."<br />"; // Last month day
}
References
PHP Manual - DateTime
PHP Manual - DateInterval
PHP Manual - DatePeriod
PHP Manual - clone
I want a loop that checks the current month, 12 months in the future and 4 months in the past.
For example: Today is 1st August 08. My loop should go through April, May, June, July, August, September, October, November, December, January, February, March, April, May, June, July, and August.
I have tried strotime but I don't know how I can loop 4 months back and 12 months in the future.
Here is my code
$i = 1;
$month = strtotime('2013-08-01');
while($i <= 12) {
$month_name = date('F', $month);
echo $month_name;
echo "<br>";
$month = strtotime('+1 month', $month);
$i++;
I think Yoshi was almost there with his answer, but using DatePeriod with DateTime is more consistent and makes for more readable code IMHO:-
$oneMonth = new \DateInterval('P1M');
$startDate = \DateTime::createFromFormat('d H:i:s', '1 00:00:00')->sub(new \DateInterval('P4M'));
$period = new \DatePeriod($startDate, $oneMonth, 16);
foreach($period as $date){
//$date is an instance of \DateTime. I'm just var_dumping it for illustration
var_dump($date);
}
See it working
This can be quite tricky, here's how I would do it:
$month = date("n", "2013-08-01") - 1; // -1 to get 0-11 so we can do modulo
// since you want to go back 4 you can't just do $month - 4, use module trick:
$start_month = $month + 8 % 12;
// +8 % 12 is the same is -4 but without negative value issues
// 2 gives you: 2+8%12 = 10 and not -2
for ($i = 0; $i < 16; $i += 1) {
$cur_month = ($start_month + $i) % 12 + 1; // +1 to get 1-12 range back
$month_name = date('F Y', strtotime($cur_month . " months"));
var_dump(month_name);
}
something like this?:
$start = -4;
$end = 12;
for($i=$start; $i<=$end;$i++) {
$month_name = date('F Y', strtotime("$i months"));
echo $month_name;
echo "<br>";
}
Your code, just slightly modified.
date_default_timezone_set('UTC');
$i = 1;
$month = strtotime('-4 month');
while($i <= 16) {
$month_name = date('F', $month);
echo $month_name;
echo "<br>";
$month = strtotime('+1 month', $month);
$i++;
}
Simplest solution:
for($i=-4; $i<=12; $i++) {
echo date("F",strtotime( ($i > 0) ? "+$i months" : "$i months") )."\n";
}
Explanation:
The loop starts at -4 and goes all the way upto 12 (total 17, including 0). The ternary statement inside strtotime() simply checks if $i is positive, and if it is, a + is inserted so that we'll get the results for strtotime("+1 months") and similar.
Ta-da!
Using DateTime is the easiest and more readable way.
I would do it like this:
$from = new DateTime('-4 month');
$to = new DateTime('+12 month');
while($from < $to){
echo $from->modify('+1 month')->format('F');
}
All the users are in the United States. I need to be able to list all weekdays besides today. So say that it's Thursday, October 7. It should start by listing Friday, October 8 and then Monday, October 11.
I know how to make sure I'm only listing weekdays when looping through, but the trouble I have is making sure tomorrow is tomorrow. In the past it's changed at about 8:00 at night eastern time. I'm thinking I'd like to have is so when it's maybe 12:00 pacific time to count it as the next day.
<?php
$current = new DateTime('now');
$last = new DateTime('saturday');
while ($current < $last) {
echo $current->format('l, F j'), "\n";
$current->modify('+1 day');
}
?>
You can use strtotime to get the date of the next days and you can use date to determine if a date is a week day :
<?php
$reference = time(); // We set today as the first day //
for ($i=0, $j=0; $i<5; $i++, $j++) {
$nextDay = strtotime('+' . $j . ' days', $reference);
if (date('w', $nextDay) > 0 && date('w', $nextDay) < 6) {
echo date('r', $nextDay), "\n";
} else {
$i--;
}
}
?>