Showing posts with label PHP DateTime. Show all posts
Showing posts with label PHP DateTime. 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)));  
        }  
 ?>


Wednesday, 1 October 2014

Get tomorrows date from date in PHP

<?php

//answer1

$datetime = new DateTime('tomorrow');
echo $datetime->format('Y-m-d H:i:s');


//answer2
$datetime = new DateTime('2013-10-02');
$datetime->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');


//answer3

$datetime = new DateTime('2013-10-02');
$datetime->add(new DateInterval("P1D"));
echo $datetime->format('Y-m-d H:i:s');


//answer4

echo (new DateTime('2013-10-02'))->add(new DateInterval("P1D"))->format('Y-m-d H:i:s');
                               
$tomorrow_timestamp = strtotime('+1 day', strtotime('2013-10-02'));


echo $tomorrow;



//answer5
//1 Day = 24*60*60 = 86400
echo date("d-m-Y", time()+86400);
?>


<?php

//You could also use the DateTime class. today as 10/01

$date = new DateTime('tomorrow');

echo $date->format('m/d/Y'); # 10/01/2013
//To set the time.

$date->setTime(12, 0 ,0);

echo $date->format('m/d/Y H:i:s'); # 10/02/2013 12:00:00
//In your case.

echo $date->format('l jS F');






echo date('l jS F', strtotime("+1 day"));


$date = date("l jS F", strtotime("tomorrow"));
echo $date;  

?>

Friday, 19 September 2014

PHP get day N by this date format

 <?php
 $myDate = "19-sept-2014";
$which_day = DateTime::createFromFormat('d-M-Y', $myDate);
echo "Which Day > " . $which_day->format('N');
?>

N -> Refers to day number in the week.

0-Sunday
1-Monday
2-Tuesday
3.Wednesday
4.Thursday
5.Friday
6.Saturday

Output for the above script is: Which Day > 5

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