I am calculated the date in 6 months time like this...
$date = new DateTime('01/02/2019');
$date->add(new DateInterval('P6M'));
echo $date->format('d/m/Y') . "\n";
This is working as far as I can tell, does anybody know of a way to get it to output an array of dates in this new period?
Does DateTime have anything built in?
You need to loop through all the dates, as there is no native function to do this.
You can create a loop that would append each date to an array, and increment the date until it reaches the end-date.
function get_interval($startDate, $endDate) {
$result = [];
while ($startDate < $endDate) {
$currentDate = (clone $startDate);
$result[] = $currentDate;
$startDate->modify("+1 day");
}
return $result;
}
$date = new DateTime('01/02/2019');
$enddate = (clone $date)->add(new DateInterval('P6M')); // Note that the object is cloned
// Otherwise we modify the original $date
print_r(get_interval($date, $enddate));
Live demo at https://3v4l.org/aLf5o
Related
I need to calculate ages depended on birthday from a table of customers.
This is my code:
// customer age
function calculate_customer_age($startDate,$endDate){
$endDate=date(m-d-y);
$startDate= $customer['birthday'];
$fromDate= new DateTime($startDate);
$toDate= new DateTime($endDate);
echo $fromDate->diff($toDate)->days;
}
In the last row, I need to echo age calculated depended on retrieve from a table of a customer from MySQL.
You can use below code in your function, which will calculate age upto today. You can modify $today to use the end date and format you want.
$dateOfBirth = $customer['birthday'];
$today = date("Y-m-d");
$diff = date_diff(date_create($dateOfBirth), date_create($today));
echo 'Age is '.$diff->format('%y');
There are many errors in your code. There is no need to pass end date in your function. You can use the following code to get the age.
function calculate_customer_age($startDate){
$fromDate = new DateTime($startDate);
$toDate = new DateTime('today');
return $fromDate->diff($toDate)->days;
// or return $fromDate->diff($toDate)->y;
}
echo calculate_customer_age($customer['birthday']);
You had several errors in your calculate_customer_age function, instead you can use the below code to get the difference in days between two dates:
function calculate_customer_age($startDate, $endDate)
{
$startDate = DateTime::createFromFormat("m/d/Y", $startDate);
$endDate = DateTime::createFromFormat("m/d/Y", $endDate);
return $startDate->diff($endDate)->days;
}
echo calculate_customer_age("11/30/2008", "08/23/2018"); // 3553
Documentation:
http://php.net/manual/en/class.datetime.php
PHP >= 5.3.0
object oriented
$from = new DateTime('1970-02-01');
$to = new DateTime('today');
echo $from->date_diff($to)->y;
procedural
echo date_diff(date_create('1970-02-01'), date_create('today'))->y;
MySQL >= 5.0.0
SELECT TIMESTAMPDIFF(YEAR, '1970-02-01', CURDATE()) AS age
If you want to calculate customer's age in days, you should do so:
function calculate_customer_age($startDate){
$fromDate= new DateTime($startDate);
$toDate= new DateTime();
$difference = $fromDate->diff($toDate)->days;
return $difference->format('%a');
}
php code
$startDate = '05-01-1988'; // sample start date or dob
$end_Date = '23-08-2018'; // smmple end date
echo calculate_customer_age( $startDate , $end_Date );
php function that returns age -
function calculate_customer_age( $startDate , $endDate ) //function returns age
{
$diff = date_diff( date_create( $startDate ), date_create( $endDate ) );
return $diff->format('%y Years, %m Months, %d Days');
}
Sample Output :
30 Years, 7 Months, 18 Days
I'm trying to get the date every 1 year or any variable interval between two dates. My dates examples are as follows. It just seems to run forever and I cant make it work and it doesn't print anything out at the end.
$currentDate = '2014-04-15';
$endDate = '2018-04-15';
$reminder = '+1 year';
$dateArray = array();
while($currentDate <= $endDate){
$currentDate = date('Y-m-d', strtotime($reminder, strtotime($currentDate)));
array_push($dateArray, $currentDate);
}
print_r($dateArray);
please change
$dateArray = array_push($dateArray, $currentDate);
to
array_push($dateArray, $currentDate);
Because array_push, returns boolean and next time, you call this function, it tries to store string in the int/boolean type and cause error.
You can use the DateInterval class and DateTime::Add() method:
$begin_date = DateTime::createFromFormat('Y-m-d', '2014-04-15');
$end_date = DateTime::createFromFormat('Y-m-d', '2018-04-15');
$res_array = array();
while ($begin_date < $end_date)
{
$begin_date->Add(DateInterval::createFromDateString('1 year'));
$res_array[] = clone($begin_date);
}
echo ("<pre>"); print_r($res_array); echo ("</pre>");
Thats the Object oriented style.
I have $date1 = '2014-09-01' and $date2 = '2015-02-01'. Can I get months and years from $date1 to $date2 like this:
2014-sep
2014-oct
2014-nov
2014-dec
2015-jan
2015-feb
Instantiate DateTime objects and loop through them:
$date1 = new \DateTime('2014-09-01');
$date2 = new \DateTime('2015-02-01');
while ($date1 <= $date2) {
echo $date1->format('Y-M') . '<br>';
$date1->add(new \DateInterval('P1M')); // increase by one month
}
Result:
2014-Sep
2014-Oct
2014-Nov
2014-Dec
2015-Jan
2015-Feb
$date1 = new DateTime("2014-09-01");
$date2 = new DateTime("2015-02-01");
while ($date1 <= $date2) {
echo $date1->format("Y-M")."\n";
$date1->modify("+1 month");
}
Result:
2014-Sep
2014-Oct
2014-Nov
2014-Dec
2015-Jan
2015-Feb
Use DateTime and DateInterval class with couple of methods like add() to increment counter and format() to date formating using simple while loop
$startDate= new DateTime("2014-09-01");
$endDate = new DateTime("2015-02-01");
$oneMonth=new DateInterval('P1M'); //for 1 month interval
while ($startDate <= $endDate) {
print $startDate->format("Y-M")."\n"; //date formating as your requirement
$startDate->add($oneMonth); //increment counter by 1 month
}
Whats the best method to find the date in date('m.d.Y') format that is 1 day ago from another date in date('m.d.Y') format in PHP? Must be applicable to v5.1.6.
I need to build a recursive method to iterate through previous days.
Thanks!
------------Revision -------
$date = date('m.d.Y');
$date = date('m.d.Y', strtotime($date .' -1 day'));
The doesn't seem to work :(
The date must begin and end in the same format 'm.d.Y'
You really should be looking at using DateTime, DateInterval, and DatePeriod classes for this. As an example:
$start_date = '12.31.2013'; // your input date
$start_date_time = DateTime::createFromFormat('m.d.Y', $start_date);
$one_day_interval = new DateInterval('P1D');
// iterate X number of days example
$iteration_days = 30; // some value for number of iterations you want
$count_period = new DatePeriod($start_date_time, $one_day_interval, $iteration_days);
foreach($count_period as $day) {
// do something
}
// iterate between start and end date example
$end_date = '3.31.2014';
$end_date_time = DateTime::createFromFormat('m.d.Y', $end_date);
$date_period = new DatePeriod($start_date_time, $one_day_interval, $end_date_time);
foreach($date_period as $day) {
// do something
}
$date = DateTime::createFromFormat('m.d.Y', $yourDate);
$date->sub(new DateInterval('P1D');
try like this:
$date =date("Y-m-d"); //set your date
echo date('m.d.Y', strtotime($date .' -1 day'));
demo : https://eval.in/107144
What's the cleanest way to use a loop in PHP to list dates in the following way?
2011_10
2011_09
2011_08
2011_07
2011_06
...
2010_03
2009_02
2009_01
2009_12
2009_11
The key elements here:
Should be as simple as possible - I would prefer one for loop instead of two.
Should list this month's date as the first date, and should stop at a fixed point (2009-11)
Should not break in the future (eg: subtracting 30 days worth of seconds will probably work but will eventually break as there are not an exact amount of seconds on each month)
Had to make a few tweaks to the solution:
// Set timezone
date_default_timezone_set('UTC');
// Start date
$date = date('Y').'-'.date('m').'-01';
// End date
$end_date = '2009-1-1';
while (strtotime($date) >= strtotime($end_date))
{
$date = date ("Y-m-d", strtotime("-1 month", strtotime($date)));
echo substr($date,0,7);
echo "\n";
}
Maybe this little code does the thing? :
more complicated situations.
<?php
// Set timezone
date_default_timezone_set('UTC');
// Start date
$date = '2009-12-06';
// End date
$end_date = '2020-12-31';
while (strtotime($date) <= strtotime($end_date)) {
echo "$date\n";
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
?>
The credit goes to: http://www.if-not-true-then-false.com/2009/php-loop-through-dates-from-date-to-date-with-strtotime-function/
This is what im guessing your asking for cause it doesnt really make sense......
$startmonth = date("m");
$endmonth = 7;
$startyear = date("Y");
$endyear = 2012;
//First for loop to loop threw years
for($i=$startyear; $i<=$endyear; $i++, $startmonth=0) {
//Second for loop to loop threw months
for($o=$startmonth; $o<=12; $o++) {
//If statement to check and throw stop when at limits
if($i == $endyear && $o <= $endmonth)
echo $i."_".$o."<br/>";
else
break;
}
}
Will output:
2012_0
2012_1
2012_2
2012_3
2012_4
2012_5
2012_6
2012_7
PHP 5.3 introduces some great improvements to date/time processing in PHP. For example, the first day of, DateInterval and DatePeriod being used below.
$start = new DateTime('first day of this month');
$end = new DateTime('2009-11-01');
$interval = new DateInterval('P1M');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
echo $date->format('Y_m') . PHP_EOL;
}