I have a variable called $conn_date which value is 2016-09-18.
Now I want to show all months from 2016-09-18 to till now. But using bellow php code I can't get current month.
$begin = new DateTime($conn_date); // value is : 2016-09-18
$end = new DateTime( date("Y-m-d") );
$interval = DateInterval::createFromDateString('1 Month');
$period = new DatePeriod($begin, $interval, $end);
$ok = array();
foreach ( $period as $k=>$dt ) {
if ( $dt->format("Y-m-d") ) {
$ok[] = $dt->format("Y-m-d");
}
}
echo '<pre>';
print_r($ok);
echo '</pre>';
Output :
Array
(
[0] => 2016-09-18
[1] => 2016-10-18
[2] => 2016-11-18
[3] => 2016-12-18
)
I want to show current months too, How can I do this ?
$begin = new DateTime($conn_date); // value is : 2016-09-18
$end = new DateTime( date("Y-m-d") );
$end->add(new DateInterval("P1M"));
$interval = DateInterval::createFromDateString('1 Month');
$period = new DatePeriod($begin, $interval, $end);
$ok = array();
foreach ( $period as $k=>$dt ) {
if ( $dt->format("Y-m-d") ) {
$ok[] = $dt->format("Y-m-d");
}
}
echo '<pre>';
print_r($ok);
echo '</pre>';
//Here "P1M" is a one-month duration, Visit http://php.net/manual/en/datetime.add.php
You just need to add one interval to your end date. In your case $end->modify('1 month')
So the full code:
$begin = new DateTime('2016-09-18');
$end = new DateTime();
$unitQty = '1 month';
$interval = DateInterval::createFromDateString($unitQty);
$period = new DatePeriod($begin, $interval, $end->modify($unitQty));
$ok = array();
foreach ( $period as $k=>$dt ) {
if ( $dt->format("Y-m-d") ) {
$ok[] = $dt->format("Y-m-d");
}
}
$a = "2007-01-01";
$b = "2008-02-15";
$i = date("Ym", strtotime($a));
while($i <= date("Ym", strtotime($b))){
echo $i."\n";
if(substr($i, 4, 2) == "12")
$i = (date("Y", strtotime($i."01")) + 1)."01";
else
$i++;
}
Related
If you're using PHP
$start = new DateTime('1-1-2017');
$end = new DateTime('4-1-2017');
split date to 3
result :
array(
array('start'=> 1-1-2017,'end'=>2-1-2017),
array('start'=> 2-1-2017,'end'=>3-1-2017),
array('start'=> 3-1-2017,'end'=>4-1-2017)
);
thanks
Try this..!!
Use DateTime to iterate dates :
$start = new DateTime('1-1-2017');
$end = (new DateTime('4-1-2017'))->modify('+1 day');
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format("d-m-y") . "<br>\n";
}
Suppose that i have
$time1 = 1492894800 //23/04/2017
$time2 = 1503521999 //23/08/2017
I would like to get timestamps grouped by months such that
get timestamp with rangess between months preserving first and last month of $time1 and $time2 such that i should have
1 23/04/2017 to 30/04/2017
2 01/05/2017 - 31/05/2017
....... //continue full months and lastly
01/08/2017 - 23/08/2017
i have tried
$timearrays = $this->getMontTimestampRanges($time1, $time2);
public function getMontTimestampRanges($ts1, $ts2){
$start = (new DateTime(date("Y-m-d", $ts1)));
$end = (new DateTime(date("Y-m-d", $ts2)));
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start, $interval, $end);
$timestamps = [];
foreach ($period as $dt) {
//am stuck here on how to group based on the months
..push to array
}
return $timestamps;
}
so the final array i would like it to be of the form:
$timestamps = [
0=>["from":, "to": ] //first month(april)
1=>[ ] //second month may
]
How do i go about this?
After clarifying your query on my last answer, I have decided to delete my answer and post a new answer.
$start = 1492894800; //23/04/2017
$end = 1503521999; //23/08/2017
$steps = date('Ym', $end) - date('Ym', $start);
$timestamps = array();
for($i = 0; $i <= $steps; $i++) {
$base = strtotime("+{$i} months", $start);
$timestamps[] = array(
'from' => $i == 0 ? date('Y-m-d', $start) : date('Y-m-01', $base),
'to' => $i == $steps ? date('Y-m-d', $end) : date('Y-m-t', $base)
);
// If we want timestamps
// $timestamps[] = array(
// 'from' => strtotime(date('Y-m-01', $base)),
// 'to' => strtotime(date('Y-m-t', $base))
// );
}
print_r($timestamps);
Ideone link
I edited your code
Try this:
<?php
$time1 = 1492894800 ;//23/04/2017
$time2 = 1503521999; //23/08/2017
$timearrays = getMontTimestampRanges($time1, $time2);
var_dump( $timearrays);
function getMontTimestampRanges($ts1, $ts2){
$start = new DateTime(date("Y-m-d", $ts1));
$end = new DateTime(date("Y-m-d", $ts2));
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
$timestamps = [];
$startM = $period->getStartDate()->format('m');
$endM = $period->getEndDate()->format('m');
foreach ($period as $dt) {
//am stuck here on how to group based on the months
//start
if($startM == $dt->format('m'))
{
$timestamps[] = array('start'=>$start->format('Y-m-d'),'end'=>$dt->format('Y-m-t'));
}
elseif($endM == $dt->format('m'))
{
$timestamps[] = array('start'=>$dt->format('Y-m').'-01','end'=>$end->format('Y-m-d') );
}
else
{
$timestamps[] = array('start'=>$dt->format('Y-m').'-01','end'=>$dt->format('Y-m-t'));
}
}
return $timestamps;
}
http://sandbox.onlinephpfunctions.com/code/8527e45427fd0114f1e058fffc1885e460807a0a
Hope it helps.
I am trying this code:
$start_date = "2015-08-19";
$end_date = "2016-02-19";
$begin = new DateTime( $start_date );
$end = new DateTime( $end_date);
$interval = new DateInterval('P1Y'); // 1 Year interval
$period = new DatePeriod($begin, $interval, $end);
foreach ( $period as $dt ){
echo $dt->format( "Y" );
}
$intervals = new DateInterval('P1M'); // 1 month interval
$periods = new DatePeriod($begin, $intervals, $end);
foreach ( $periods as $dts ){
echo $dts->format( "m" );
}
I'm getting output like this:
year:2015
Month:08,09,...,01
In this output I'm not getting year:2016 and month:02. I want my output like this:
year:2015,2016
Month:08,09,...,01,02
How can I get this?
And if my end date is "2016-08-20"
than i m getting year:2016 but not getting month :08
Note that my start date and end date is not fixed.
As mentioned before, the end date is not included
You may also modify the end date like that
$end = $end->modify( '+1 day +1 year' );
See php's DateTime modify for more info.
Update
If end year is more than 1 year bigger than the begin date then adding a year will indeed return a year more than what we want.
A solution is to check and add the year only if is needed like that:
if($begin > (new DateTime( $end_date))->modify('-1 year')) {
$end->modify( '+1 year' );
}
$end->modify('+1 day');
You can use the following code to achieve this:
<?php
$start = (new DateTime('2015-12-02'))->modify('first day of this month');
$end = (new DateTime('2016-05-06'))->modify('first day of this month');
//For Year
$interval = DateInterval::createFromDateString('1 year');
$period = new DatePeriod($start, $interval, $end);
echo 'Year: ';
foreach ($period as $dt)
{
echo $dt->format("Y") . ",";
}
//For Month
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
echo '<br/>Month: ';
foreach ($period as $dt)
{
echo $dt->format("m") . ",";
}
?>
Alternatively, you can display it nicely in Y-m format by the following code:
$start = (new DateTime('2015-12-02'))->modify('first day of this month');
$end = (new DateTime('2016-05-06'))->modify('first day of this month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt)
{
echo $dt->format("Y-m") . "<br>\n";
}
since the year 2016 and the month 02 is not completed you are not getting it.
try this
$start_date = "2015-08-19";
$end_date = "2016-02-19";
$begin = new DateTime( $start_date );
$end = new DateTime( $end_date);
$end->add(new DateInterval('P1Y1M'));
$interval = new DateInterval('P1Y'); // 1 Year interval
$period = new DatePeriod($begin, $interval, $end);
foreach ( $period as $dt ){
echo $dt->format( "Y" );
}
$intervals = new DateInterval('P1M'); // 1 month interval
$periods = new DatePeriod($begin, $intervals, $end);
foreach ( $periods as $dts ){
echo $dts->format( "m" );
}
Using the following function I can display the years and months between two dates, but how can I add the correct days for each month as another array within each month? I can't just add the days manually as I need it to account for leap years etc.
function yearMonth($start_date, $end_date)
{
$begin = new DateTime( $start_date );
$end = new DateTime( $end_date);
$interval = new DateInterval('P1M'); // 1 month interval
$period = new DatePeriod($begin, $interval, $end);
foreach ( $period as $dt )
$years[$dt->format( "Y" )][] = $dt->format( "F" );
return $years;
}
$list = yearMonth("2007-03-24", "2009-06-26");
var_dump($list);
Since nobody else answered:
function yearMonth($start_date, $end_date)
{
$begin = new DateTime( $start_date );
$end = new DateTime( $end_date);
$interval = new DateInterval('P1D'); // 1 month interval
$period = new DatePeriod($begin, $interval, $end);
$lastMonth = null;
$lastYear = null;
$aResult = array();
foreach ( $period as $dt )
{
if ($dt->format('Y') != $lastYear)
{
$lastYear = $dt->format('Y');
}
if ($dt->format('F') != $lastMonth)
{
$lastMonth = $dt->format('F');
}
if (!isset($aResult[$lastYear]))
{
$aResult[$lastYear] = array();
}
if (!isset($aResult[$lastYear][$lastMonth]))
{
$aResult[$lastYear][$lastMonth] = array();
}
$aResult[$lastYear][$lastMonth][] = $dt->format('d');
}
return $aResult;
}
On a side note I am planing to create a sort of Gantt chart style flat layout in table format of the years, months and days between dates. Do you think this is a suitable way of generating that? Or is there a better way?
This question already has answers here:
Print time in 15-minute increments between two times in the same day
(10 answers)
Closed 11 months ago.
I'm trying to set a variable with a foreach loop, every minute.
I have this code:
$dhit = 2013-02-07 04:21:01;
$dnow= 2013-02-07 10:21:01;
$begin = new DateTime( $dhit );
$end = new DateTime( $dnow );
$interval = DateInterval::createFromDateString( '1 minute' );
$period = new DatePeriod($begin, $interval, $end);
$a = 0;
foreach ( $period as $dt ) {
$a+=1;
echo $a;
}
It works very well if $dhit is at least 24 hours old (2013-02-06 04:21:01) but not if $dhit has the same date (same day, i.e. 2013-02-07) of $dnow.
What I'm doing wrong?
This works for me to, i would suspect smth with your system time maybe not being set properly.
What error do you get ?
this is the test i made and it printed 1-360 as expected
<?php
$dhit = '2013-02-07 04:21:01';
$dnow= '2013-02-07 10:21:01';
$begin = new DateTime( $dhit );
$end = new DateTime( $dnow );
$interval = DateInterval::createFromDateString( '1 minute' );
$period = new DatePeriod($begin, $interval, $end);
$a = 0;
foreach ( $period as $dt ) {
$a+=1;
print "$a\n";
}
This works fine for me and prints 123456. You forgot to place quotes around your dates.
$dhit = '2013-02-07 10:15:01';
$dnow= '2013-02-07 10:21:01';
$begin = new DateTime( $dhit );
$end = new DateTime( $dnow );
$interval = DateInterval::createFromDateString( '1 minute' );
$period = new DatePeriod($begin, $interval, $end);
$a = 0;
foreach ( $period as $dt ) {
$a+=1;
echo $a; //output 123456
}