How to Get previous days in this month PHP? - php

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/>";
}

Related

Adding month in php?

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 ?

Adding new items into PHP array and saving previous size of the array

I have the following code so far:
$months = array();
$numJoin = date("n",strtotime($me['joinTime']));
$numLast = date('n', strtotime('Dec 31'));
$numCurrent = date("n",strtotime('2016-06-01'));
array_push($months, date("F", strtotime($me['joinTime'])));
for($i = ($numJoin + 1); $i <= $numLast; $i++) {
if($numCurrent>$numJoin) {
$dateObj = date_create_from_format('!m', $i);
array_push($months, $dateObj->format('F'));
}
$numCurrent= -1;
}
What I'm trying to do here is to add into the array current month that kicks in, and save previous months in the array like for example:
Start month is -> May
June kicks in -> I add June into the array (now I should have May and June in array).
July kicks in -> I add July into the array (now I should have May, June and July in array).
How can I do this achieve this? Current solution works only for +1 month.. I can't add more than 1 month :/
P.S. New item should only be added when the new month kicks in, and previous content of the array should be saved...
Here we go, you need to check that your month is less than the current month or not. Check Online
$months = array();
$num = date("n",strtotime($me['joinTime'])); //join month number
$now = date("n"); //Current month number
for($i = $num; $i <= $now; $i++){
$dateObj = DateTime::createFromFormat('!m', $i);
array_push($months, $dateObj->format('F'));
}
print_r($months);
I'm still a little confused, but I think this is what you are after... all month names after the join month and until current month...
$me = array('joinTime'=>'2016-03-01');
$dtCurrent = strtotime($me['joinTime']);
$arrMonths = array();
while($dtCurrent < time()) {
$dtCurrent = strtotime("+1 month",$dtCurrent);
$arrMonths[] = date('F',$dtCurrent);
}
var_dump($arrMonths);

display last days of previous month php

I searched the millions of similar posts about this but cannot find how to make it count down from 31, 30, 29, 28 etc.
I can have the previous calendar blocks show the 31st but that's all. I need it to show previous month 31st, 30, 29, etc.
updated code from Renku:
//define the variable dayCol
$dayCol = 0;
// Print last months' days spots.
for ($i=0; $i<$leadInDays; $i++) {
$lastmonth = date('d', strtotime(-$i.' day', strtotime($startDate))); // Days in previous month
print "<td width=\"14%\" height=\"25%\" class=\"calendar_cell_disabled_middle\">$lastmonth</td>\n ";
$dayCol++;
}
example :
I am writing a new loop for this.
<?php
$StartDate= date("Y-F-d",strtotime("+0 Month"));// get first day of current month
$num= 10; // how many past days you need from previous month + 1 (to remove current day)
for ($i=1; $i<$num; $i++) {
echo $prev= date('Y-m-d', strtotime(-$i.' day', strtotime($StartDate)))."<br />"; //get last days of previous month
}
?>
I am re writing it with your loop,
<?php
$dayCol = 0;
$leadInDays = 5; // (just for February cuz theres 5 blanks before its the 1st of Feb)
$StartDate= date("Y-F-d",strtotime("+0 Month"));
// Print last months' days spots.
for ($i=1; $i<($leadInDays+1); $i++) {
$lastmonth = date('d', strtotime(-$i.' day', strtotime($StartDate))); // Days in previous month
print "<td width=\"14%\" height=\"25%\" class=\"calendar_cell_disabled_middle\">$lastmonth</td>\n ";
$dayCol++;
}
?>
Test it Here
I would use date('t') to get the number of days in said month and just loop backwards:
$month = '2013-02-05';
for($i = date('t', strtotime($month)); $i > 0; $i--) {
...
}

Get Dates of the Last 7 Days in Array In Custom Format

How do I get the exact dates of the last 7 days including today in a custom format (dd/mm)?
In the resulting array I would like to get something like (dates are examples only):
1=>11/2 (today minus 7 days)
2=>12/2 (today minus 6 days)
...
7=>17/2 (today)
function getLastNDays($days, $format = 'd/m'){
$m = date("m"); $de= date("d"); $y= date("Y");
$dateArray = array();
for($i=0; $i<=$days-1; $i++){
$dateArray[] = '"' . date($format, mktime(0,0,0,$m,($de-$i),$y)) . '"';
}
return array_reverse($dateArray);
}
Usage:
$arr = getLastNDays(7);
or
$arr = getLastNDays(7, 'd/m/Y');
You can combine the 2 functions date() and strtotime(). for example:
echo date("Y-m-d", strtotime("7 days ago"));
Try:
for ($i=0; $i<7; $i++)
{
echo date("d/m", strtotime($i." days ago")).'<br />';
}
You should be able to work out how to get them in the correct order and into an array :)
Hope that helps
time() gives you the current timestamp.
86400 seconds are one day (60 * 60 * 24).
date() gives you a custom date string.
for ($iDay = 6; $iDay >= 0; $iDay--) {
$aDays[7 - $iDay] = date('d/m', time() - $iDay * 86400);
}
Also see this example.
If you don't want the leading zeros, use 'j/n' as custom date format parameter:
for ($iDay = 6; $iDay >= 0; $iDay--) {
$aDays[7 - $iDay] = date('j/n', time() - $iDay * 86400);
}
Also see this updated example.
=== UPDATE ===
#Dagon's idea to use strtotime() to get the timestamp is great. Here the better solution:
for ($iDay = 6; $iDay >= 0; $iDay--) {
$aDays[7 - $iDay] = date('j/n', strtotime("-" . $iDay . " day"));
}
And an example.

calculate the dates that are between two days of the week

I need an algorithm that calculates dates that are between two days of the week!
for example i have
start date: 23-04-2012 and end date: 27-04-2012
now i want receive an array with this structure:
$arr = array(
'23-04-2012',
'24-04-2012',
'25-04-2012',
'26-04-2012',
'27-04-2012',
'28-04-2012'
);
thanks!
You could use the DateTime::Diff function (documentation here).
Pass your two dates into the function.
Get the return value as the number of days.
Add one to it (so as to include both the start and end dates).
Put together a for(int i = 0; i <= $days_difference; i++) loop.
Increment the date by one day each time and add it to the array.
$start = '23-04-2012';
$end = '27-04-2012';
$startTs = strtotime("$start 00:00:00");
$endTs = strtotime("$end 00:00:00");
$days = array();
$day = $startTs;
$i = 0;
while ($day <= $endTs) {
$days[] = date('d-m-Y', $day);
$i++;
$day = mktime(0, 0, 0, date('n', $startTs), date('j', $startTs) + $i, date('Y', $startTs));
}
var_dump($days);
This could help you.
$startdate=explode("-","23-04-2012");
$enddate=explode("-","27-04-2012");
$i;
$arr=array();
for($i=$startdate[0];$i<=$endate[0];$i++)
{
$dd=$i.'-'.$startdate[1].'-'.$startdate[2];
array_push($arr,$dd);
}
print_r($arr);

Categories