month adding problem
<?php
$showMonthsQty = 3;
for($i = (1-$showMonthsQty); $i <= 0; $i++)
{
echo $date = date("Y-m-1", strtotime(" +$i months"));
}
?>
when it run's im not geting the desired answer.
OUTPUT
2018-03-1
2018-05-1 <----------error
2018-05-1
but i needed output is :
2018-03-1
2018-04-1
2018-05-1
how can i get this ?pls help!... tnx in advance...:)
You can use DateTime and DateInterval.
$showMonthsQty = 3;
for($i = $showMonthsQty-1; $i >= 0; $i--)
{
$date = new \DateTime(date("Y-m-1")); // First day of the current month
$date->sub(new \DateInterval(sprintf('P%sM', $i))); // Substract $i month (P%dM)
echo $date->format('Y-m-d')."<br />";
}
Output:
2018-03-01
2018-04-01
2018-05-01
Is it what you're looking for ?
Related
How to get continue date next month based on number of parameters?
Example:
$loan = 4; //count from this month
$fixedDateEveryMonth = 28;
for($i=0 $i<=$loan; $i++)
{
echo $fixedDateEveryMonth."-".date("m-y");
}
So what I expected is to get below date:
28-Apr-2019
28-May-2019
28-Jun-2019
28-Jul-2019
Any idea?
This code will do what you want. It creates a DateTime object from the start date, and then adds 1 month to the date through the loop:
$loan = 4;
$fixedDateEveryMonth = 28;
$start = new DateTime(date('Y-m-') . $fixedDateEveryMonth);
for ($i = 1; $i <= $loan; $i++) {
$start->add(new DateInterval("P1M"));
echo $start->format('d-M-Y') ."\n";
}
Output:
28-Apr-2019
28-May-2019
28-Jun-2019
28-Jul-2019
Demo on 3v4l.org
Please, i need assistance in this code.I have checked others in Stakeoverflow, but it is not combatible, hence this question. I want to generate all working /weekdays between two dates.I have found a code, but it is generating all days, including weekend. How do i eliminate the weekend from the list or ensure the list generated is ONLY for weekdays?
<?php
$start_Date = date('Y-m-d');
$end_Date = date('Y-m-d', strtotime('30 weekdays'));
//echo $start_Date."<br/>";
//echo $end_Date."<br/>";
// Specify the start date. This date can be any English textual format
$date_from = $start_Date;
$date_from = strtotime($date_from); // Convert date to a UNIX timestamp
// Specify the end date. This date can be any English textual format
$date_to = $end_Date;
$date_to = strtotime($date_to); // Convert date to a UNIX timestamp
// Loop from the start date to end date and output all dates inbetween
$c = 0;
for ($i = $date_from; $i <= $date_to; $i += 86400) {
$c++;
echo $c . "=> " . date("Y-m-d", $i) . '<br />';
}
I expect 30days to be generated but with this code, I am getting 42days . Weekend has been added,instead of weekdays ONLY .
Just add this to your loop:
$w = date('w',$i);// day of week - Sunday == 0, Saturday == 6
if($w == 0 || $w == 6){
continue;
}
DEMO
Your code is almost working only have to add a if checking in your code
your code
for ($i = $date_from; $i <= $date_to; $i += 86400) {
$c++;
echo $c . "=> " . date("Y-m-d", $i) . '<br />';
}
please replace with that one
for ($i = $date_from; $i <= $date_to; $i += 86400) {
$day = date("w", $i);
if($day != 0 && $day!= 6){ // will continue if not Sunday or Saturday
$c++;
echo $c . "=> " . date("Y-m-d", $i) . '<br />';
}
}
You also can take help from php.net
Thanks
You may need to get the day of the week, like date("D"), then use it in your for loop to check..something like this?:
$Weekends = array("Sat","Sun");
for....
$DayOfWeek = date("D",$i);
if(!in_array($DayOfWeek, $Weekend)){
// increment...
}
How to Get previous days in this month with PHP?
ex : current date = 06/05/2015
previous days Show ==>
05/05/2015
04/05/2015
03/05/2015
02/05/2015
01/05/2015
cordially
Something like this:
$date = DateTime::createFromFormat("d/m/Y", "06/05/2015");
$previousDates = array();
$maxDay = $date->format("d");
for($i = 1; $i < $maxDay; $i++) {
$previousDates[] = $date->modify("-1 day")->format("d/m/Y");
}
Sowing previous 30 days from current days:
for($i=-1; $i>=-30;$i--)
echo date('d/m/Y',strtotime($i." days"));
You can change date format if you want.
<?php
$currentDate = date('d');
for($inc = 1;$inc <= ($currentDate-1);$inc++){
echo date('d/m/Y',strtotime("-".$inc." days")).'<br/>';
}
?>
Try above code. Hope this will help you.
Output:
05/05/2015
04/05/2015
03/05/2015
02/05/2015
01/05/2015
This is how you get a previous day using php:
$prev_day = date('d.m.Y',strtotime("-1 days"));
to get all the previous days from the specified day, you need to do a loop:
$currentday = date("d");
for($i=1; $i < $currentday ;$i++)
{
echo date('d.m.Y',strtotime($i."- days"))."<br/>";
}
Hi i currently have this piece of code,
$date = time();
for ($n = 1; $n <= date('y-m-d', $date); $n++) {
//Code to run
}
However what I would really like it to do is for $n for today for example would be 2015-01-14 yesterday 2015-01-13 and so on for the past 30 days, so that i can use $n and get the full date.
Any help would be greatly appreciated.
<?php
$d = array();
for($i = 0; $i < 30; $i++)
$d[] = date("y-m-d", strtotime('-'. $i .' days'));
?>
hope this will help you.
You can use the DateTime class for this.
$date = new DateTime();
$dates = [];
for($i = 0; $i < 30; $i++) {
$date->sub(new DateInterval('P1D'));
$dates[] = $date->format('Y-m-d');
}
print_r($dates);
Which will return
Array
(
[0] => 2015-01-13
[1] => 2015-01-12
[2] => 2015-01-11
[...]
[29] => 2014-12-15
)
Just use the code below
<?php
for ($n = 0; $n <= 30; $n++) {
echo date('Y-m-d', strtotime('-'.$n.' days'))."<br>"; // this ill print last 30 days date
}
?>
Hope this helps you
I have a trouble with strtotime in php.
$j = "2013-10-27";
for ($x = 0; $x < 100; $x++) {
$j = date('Y-m-d', strtotime($j) + 86400);
echo ' '.$j.' <br/>';
}
As the code is self explained, it will add one day to $j and then display to browser.
But when $j = "2013-10-27", it prints just one result ("2013-10-27"). If I change $j to another date, this does work, but also stucks at this date (and some other date).
I have written other code to do this work.
But does anyone know why it fails, or my code is wrong?
Thank you.
This is because you are in a timezone with daylight savings time, and on the 27th October at 1 AM the time reverted to midnight, making it a 25 hour day.
This can be reproduced by setting the timezone:
<?php
date_default_timezone_set('Europe/London');
$j = "2013-10-27";
for ($x = 0; $x < 100; $x++) {
$j = date('Y-m-d', strtotime($j) + 86400);
echo ' '.$j.' <br/>';
}
http://codepad.viper-7.com/uTbNWf
strtotime has too many limitations in my opinion. Use the more recent DateTime lib instead
$j = new DateTime('2013-10-27');
$interval = new DateInterval('P1D');
for ($x = 0; $x < 100; $x++) {
$j->add($interval);
echo $j->format('Y-m-d'), '<br>';
}