I have two dates
2016-06-22 , 2016-07-11
I need to print the date in numbers for example,
22,23,24,25,26,27,28,29,30,1,2,.....11
if the month is july to august it should print
22,23,24,25,26,27,28,29,30,31,1,2,.....11
according to month wise also in PHP.
Thank you.
This will work for you .. Look The DatePeriod class
A date period allows iteration over a set of dates and times, recurring at regular intervals, over a given period.
<?php
$begin = new DateTime( '2016-06-22' );
$end = new DateTime( '2016-07-11' );
$end = $end->modify( '+1 day' );
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
foreach($daterange as $date){
echo $date->format("d") . "<br>";
}
?>
LIVE EXAMPLE : CLICK HERE
You have to iterate over date between start and end date and print it in format d.
$fromDate = new DateTime('2016-06-22');
$toDate = new DateTime('2016-07-11');
$days = array();
while($fromDate <= $toDate) {
$days[] = $fromDate->format('d');
$fromDate->modify('tomorrow');
}
echo implode(',', $days);
Try:
function createDateRangeArray($strDateFrom,$strDateTo)
{
// inclusive array of the dates between the from and to dates.
// could test validity of dates here but I'm already doing
// that in the main script
$aryRange=array();
$iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2), substr($strDateFrom,8,2),substr($strDateFrom,0,4));
$iDateTo=mktime(1,0,0,substr($strDateTo,5,2), substr($strDateTo,8,2),substr($strDateTo,0,4));
if ($iDateTo>=$iDateFrom)
{
array_push($aryRange,date('d',$iDateFrom)); // first entry
while ($iDateFrom<$iDateTo)
{
$iDateFrom+=86400; // add 24 hours
array_push($aryRange,date('d',$iDateFrom));
}
}
return $aryRange;
}
$arr = createDateRangeArray("2016-06-22","2016-07-11");
echo implode(",",$arr);
in mysql
select date_format(my_date_column, '%d$) from my_table
in php
$date = new DateTime('2016-06-22');
echo $date->format('d');
echo the day number
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 need to get immediate date like if i select date is 05 then output will be 2017-07-05 cause select date already passed
if i select 12 then output will be 2017-06-12 cause this date future date
final if i select previous date of current month then output will be next month same date and if i select future date of current month then output will be same month
i have tired but not working this
$today = date("Y-m-d");
$next_payment_date = date('Y-m-d', strtotime('+1 month', $today));
or
$time = time();
date("Y-m-d", mktime(0,0,0,date("n", $time),date("j",$time)- 1 ,date("Y", $time)));
thanks in your advance
One more option:
https://3v4l.org/Me2Kh
$input = 12;
$day = date("d");
if ($input > $day){
$date = date("Y-m-"). str_pad($input,2,"0", STR_PAD_LEFT);
}else{
$date = date("Y-m-",strtotime("+1 month")). str_pad($input,2,"0", STR_PAD_LEFT);
}
echo $date;
I use str_pad to keep two digit day number.
Try this -
<?php
$day = '05';
$today = date('Y-m-d');
$supplied = date('Y-m-'.$day);
if($today>$supplied){
$final = date('Y-m-d', strtotime("+1 months", strtotime($supplied)));
}
else{
$final = $supplied;
}
echo $today;
echo '<br />';
echo $supplied;
echo '<br />';
echo $final;
What I'm doing here -
Comparing the current and supplied date
Based on comparison, if supplied date is smaller, I'm adding 1 month else dislpaying the supplied date.
use this,
$today = date('Y-m-d');
$nextDate = date('Y-m-d', strtotime('+1 month'));
or $nextDate = date('Y-m-d', strtotime('+1 month', strtotime($today));
I think this may help.
I would consider using DateTime and it's add method for a DateInterval.
$date = new \DateTime('now', new \DateTimeZone('America/New_York'));
$interval = new \DateInterval('P1M');
$date->add($interval);
Here are the supported DateTimeZone values. Make sure to set that to the applicable time zone.
Edit:
DateTime is mutable, so please keep that in mind.
Try this code :
<?php
$selected_date = '2017-06-05';
$current = date('Y-m-d');
//echo $current;
if($selected_date < $current)
{
$newDate = date('Y-m-d',strtotime($selected_date."+1 month"));
echo $newDate; // gives 2017-07-05
}else if($selected_date > $current)
{
$newDate = $current;
echo $newDate; // gives 2017-06-07
}
?>
From what you described in the points at beginning of the question, you could achieve it this way:
$selectedDate = new DateTime('2016-06-05 00:00:00');
$now = new DateTime('now');
$now->setTime(0, 0, 0);
if ($selectedDate < $now) { // Selected date is in past
// Set month and year to current
$selectedDate->setDate(
date('Y'),
date('m'),
$selectedDate->format('d')
);
// Add 1 month
$selectedDate->add(new DateInterval('P1M'));
}
// If selected date is current or in future we do nothing
echo $selectedDate->format('Y-m-d');
For input 2017-06-05 it will return 2017-07-05 as expected, and for current or future date will return the date that was selected. Works also for any past date like 2016-04-05
I need to create a date-time with specific day each month between a start date and a end date.
Example :
Specific day : 04
Start : 2016-12-22
End : 2017-03-06
Output needed : 2016-01-04 ; 2016-02-04 ; 2016-03-04
How can I do that?
You can create a DatePeriod object and iterate over it to get all the dates you need:
$start = new DateTime('2016-12-22');
$end = new DateTime('2017-03-06');
// Compute the occurrence on the same month as $start
$first = new DateTime();
$first->setDate($start->format('Y'), $start->format('m'), 4);
// If it is in the past of $start then the first occurrence is on the next month
if ($first < $start) {
$first->add(new DateInterval('P1M'));
}
// Create a DatePeriod object that iterates between $first and $end
// in increments of 1 month
$period = new DatePeriod($first, new DateInterval('P1M'), $end);
// Run through the list, process each item
foreach ($period as $day) {
echo($day->format('Y-m-d')."\n");
}
I found it ! This following code is working for me :
$d1 = new DateTime("2016-12-22");
$d2 = new DateTime("2017-03-06");
$dd = '04';
if ($dd < $d1->format('d') ) {
$d1->add(new \DateInterval('P1M'));
$date = $dd.'-'.$d1->format('m').'-'.$d1->format('Y');
} else {
$date = $d1->format('Y').'-'.$d1->format('m').'-'.$dd;
}
$date = new DateTime($date);
print_r($date);echo('<br />');
while ($date->add(new \DateInterval('P1M')) <= $d2){
$d1->add(new \DateInterval('P1M'));
$date = $dd.'-'.$d1->format('m').'-'.$d1->format('Y');
$date = new DateTime($date);
print_r($date);echo('<br />');
}
I found the following code from another post which gives me what I want except I would like to be able to grab each day as a variable so that I can use them in form fields.
Can anyone tell me how to achieve this?
<?php
$monday = new DateTime('monday');
// clone start date
$endDate = clone $monday;
// Add 7 days to start date
$endDate->modify('+7 days');
// Increase with an interval of one day
$dateInterval = new DateInterval('P1D');
$dateRange = new DatePeriod($monday, $dateInterval, $endDate);
foreach ($dateRange as $day) {
echo $day->format('Y-m-d')."<br />";
}
?>
The results of the above are this:
2015-02-16
2015-02-17
2015-02-18
2015-02-19
2015-02-20
2015-02-21
2015-02-22
Many thanks,
John
$monday = new DateTime('monday');
// clone start date
$endDate = clone $monday;
// Add 7 days to start date
$endDate->modify('+7 days');
// Increase with an interval of one day
$dateInterval = new DateInterval('P1D');
$dateRange = new DatePeriod($monday, $dateInterval, $endDate);
foreach ($dateRange as $day) {
echo "<input type='checkbox' value = {$day->format('Y-m-d')}>" . $day->format('Y-m-d')."<br />";
}
I have found a problem with using DatePeriod which might be because I am stupid lol.
As you can see I have added +1 to my end date because I want to include the last date of the range.
But my issue is when I have ending date 31 it makes it to 32 which is not a date so it throws out an error.
Is there a way to include the ending date or to make the +1 work?
$period = new DatePeriod(
new DateTime($event['startyear'].$event['startmonth'].$event['startdate']),
new DateInterval('P1D'),
new DateTime($event['endyear'].$event['endmonth'].$event['enddate'] +1)
);
foreach ($period as $savedDate) {
echo $savedDate;
}
You should create the date object for the initial date (without the +1) and then increment the day of that object by 1 day.
For example:
$date1 = new DateTime($event['endyear'].$event['endmonth'].$event['enddate']);
$date2 = new DateTime($event['endyear'].$event['endmonth'].$event['enddate']);
$date2->modify('+1 day');
$period = new DatePeriod($date1, new DateInterval('P1D'), $date2);
What about this:
$endDate = mktime(0,0,0, $event['endmonth'], $event['enddate'], $event['endyear']);
$counter = 0;
while($endDate >= $date = mktime(0,0,0, $event['startmonth'], $event['startdate']+$counter++, $event['startyear']) ) {
echo date('Y/m/d', $date);
}