Showing posts with label PHP DateInterval. Show all posts
Showing posts with label PHP DateInterval. Show all posts

Friday, 19 June 2015

PHP: Date function to display all dates between two dates

There is the DatePeriod class.
EXAMPLE:
$begin = new DateTime('2013-02-01');
$end = new DateTime('2013-02-13');

$daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);

foreach($daterange as $date){
    echo $date->format("Y-m-d") . "<br>";
}
(P1D stands for period of one day, see DateInterval for further documentation)
Example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
table,th,td{
    border:1px solid black;
}
th,td{
    height:40px;
    width:140px;
    font-weight:bold;
}
</style>
</head>

<body>
<?php
$start_date = date('Y-m-d',time()-84600);
//echo date('d/m/Y', strtotime('+2 months'));
//echo date('M d Y',$date);
$end_date = date('Y-m-d',strtotime('+3 months'));
//echo date('d/m/Y', strtotime('+1 day'));

//echo strtotime('+1 day');
$begin = new DateTime($start_date);
$end = new DateTime($end_date);

$daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);


?>
<table cellpadding="0" cellspacing="0" width="100%">
    <thead>
        <tr>
                <th>#</th>
                <td>Date</td>
                <td>SM</td>
                <td>Auditing</td>
                <td>Accounts</td>
                <td>IT</td>
        </tr>
    </thead>
    <tbody>
    <?php
    $i=1;
    foreach($daterange as $date){ ?>
        <tr>
            <td rowspan="2"><?php  echo $i; ?></td>
            <td rowspan="2"><?php  echo $date->format("M d Y"); ?></td>
            <td>test</td>
            <td>test</td>
            <td>test</td>
            <td>test</td>
        </tr>
        <tr>
            <td>test</td>
            <td>test</td>
            <td>test</td>
            <td>test</td>
        </tr>
       
        <?php $i++; } ?>
    </tbody>
</table>
</body>
</html>


Another way:
<?php 
  $day = 86400; // Day in seconds  
        $format = 'Y-m-d'; // Output format (see PHP date funciton)  
        $sTime = strtotime($start_date); // Start as time  
        $eTime = strtotime($end_date); // End as time  
        $numDays = round(($eTime - $sTime) / $day) + 1;  
        $days = array();  

        for ($d = 0; $d < $numDays; $d++) {  
            $days[] = date($format, ($sTime + ($d * $day)));  
        }  
 ?>


Friday, 19 September 2014

PHP - increment date


I want to increment a date , let's say today : $today = date("Y-m-d"), by 3 months during 3 years.
Example : 2014-09-19 -> incremented by 3 months becomes 2014-12-19 -> incremented by 3 months becomes 2015-02-19 and so on until my date it's smaller or equal with 3 years from that date , in our example 2017-09-19.

<?php
 //using DatePeriod
$begin = new DateTime('2014-09-16'); // set the starting date
$end = new DateTime('2017-09-16'); // set the ending date
$interval = new DateInterval('P3M'); // 3 months interval
$range = new DatePeriod($begin, $interval, $end); // set the period
foreach($range as $date) {
    // so foreach three months, this will loop until the end date
    echo $date->format('Y-m-d') . '<br/>'; 
}
 ?>

The output will be:
2014-09-19
2014-12-19
2015-03-19
2015-06-19
2015-09-19
2015-12-19
2016-03-19
2016-06-19
2016-09-19
2016-12-19
2017-03-19
2017-06-19

 <?php
//another way
$today = new DateTime('2014-09-19');
$formatted = $today->modify("+3 months");
echo $formatted->format('Y-m-d');

 ?>
 <?php
//another way
$startDate = date("Y-m-d");
$endDate = date('Y-m-d', strtotime("+3 years", strtotime($startDate )));
echo $startDate."<br/>";
while(strtotime($startDate) < strtotime($endDate) ){
    $startDate = date('Y-m-d', strtotime("+3 months", strtotime($startDate )));
    echo $startDate."<br/>";
} 
 ?>