How to get all dates between two dates [duplicate] - php

This question already has answers here:
PHP: Return all dates between two dates in an array [duplicate]
(26 answers)
Closed 7 years ago.
I have check-in column and check-out column in my mysql table. I am posting this dates with UI-Datepicker.
eg:
I am trying to block the dates in the datepicker. I have dates like this in database
check in = 2015-06-15
check out = 2015-06-20
Now i want to get dates like this
2015-06-15,
2015-06-16,
2015-06-17,
2015-06-18,
2015-06-19,
2015-06-20
How to get dates like above I mentioned. So please help me and resolve my problem.

<?php
$date_from = strtotime("10 September 2000");
$date_to = strtotime("15 September 2000");
function list_days($date_from,$date_to){
$arr_days = array();
$day_passed = ($date_to - $date_from); //seconds
$day_passed = ($day_passed/86400); //days
$counter = 1;
$day_to_display = $date_from;
while($counter < $day_passed){
$day_to_display += 86400;
//echo date("F j, Y \n", $day_to_display);
$arr_days[] = date('o-m-d',$day_to_display);
$counter++;
}
return $arr_days;
}
print_r(list_days($date_from,$date_to));
?>

Related

Increment days in PHP MySQL date [duplicate]

This question already has answers here:
Adding days to $Date in PHP
(12 answers)
Closed 5 years ago.
I have a table in MySQL with a date field (called NDate) which contains standard date values ("2017-04-17","2017-04-18", etc.).
Through PHP webpage, I am trying to take the system date (say today is 2017-04-17), and then pull all rows from the above table where NDate="2017-04-17". No issues till here.
I have a requirement to increment the day (starting today and going on for next 10 days - i.e. 2017-04-17 to 2017-04-26), and for each day report entries under a different heading like "Entries for 2017-04-17" which will list all rows having NDate 2017-04-17, "Entries for 2017-04-18" which will list all rows having NDate 2017-04-18.
I was trying to use a for loop with PHP date_modify function to increment the days one by one, but it is not showing any results.
Here are the selected pieces of code:
date_default_timezone_set('US/Eastern');
$datev = date("Y-m-d");
for ($x = 0; $x <= 10; $x++)
{
$datev=date_modify($date,"+$x days");
echo "before date format<br>"; // echo statement 1
echo "date is: $datev <br>"; // echo statement 2
$sql = "SELECT * FROM tablename where Ndate='$datev'";
echo "before result<br>"; // echo statement 3
...
...
...
}
Output on webpage shows only statement 1. But echo stats 2 and 3 are not printed.
You can increment days using strtotime function as a parameter to date function.
For 10 days, you can use for loop, to build an array of days. Then iterate over it, to execute queries you need.
$today = date('Y-m-d');
$dates=array($today);
for($i=1;$i<10;$i++) {
$NewDate=date('Y-m-d', strtotime("+".$i." days"));
$dates[]=$NewDate;
}
foreach($dates as $dt) {
// sql stuff here
echo "date is: $dt <br>";
$sql = "SELECT * FROM tablename where Ndate='$dt'";
echo "before result<br>";
// .....
}
This code should work for your case. If any problems, just let me know.
Try this:
$start = strtotime(date('Y-m-d'));
$end = strtotime(date('Y-m-d', strtotime('+10 days')));
while($start <= $end)
{
$date = date('Y-m-d', $start);
//use $date to do stuff
//SELECT * FROM tablename where Ndate='$date'
$start = strtotime("+1 day", $start);
}

Get the day number from a date [duplicate]

This question already has answers here:
PHP - How to get year, month, day from time string
(5 answers)
Closed 7 years ago.
I want to get the day number of a date.
For example my date is: 2015-12-31, it should return 365 and 2015-01-1 would be 1.
How can I do that?
EDIT: date('z') does not work for me, I dont want the current date
Use strtotime() — to parse about any English textual datetime description into a Unix timestamp. And getdate() — Get date/time information.
<?php
$day = getdate(strtotime("2015-12-31"));
echo $day['yday']+1; // output 365
?>
Try
<?php
$today = getdate(strtotime('2015-12-31'));
echo '<pre>';
print_r($today['yday'] + 1);
?>
Read GetDate
You can try it.
<?php
$day = 31;
$mon = 12;
$year = 2015;
$sum = 0;
$days = 0;
$month_day = array(31,28,31,30,31,30,31,31,30,31,30,31);
for ( $i = 0; $i < $mon; $i++){
$sum += $month_day[$i];
}
$days = $sum - ($month_day[$mon-1] - $day);
echo "$days";
?>
You may get day, mon, year from your expected date and make it integer. You should check the year is leap year or not. It may vary one day if it is a leap year. I think you shall do it.

Select all the dates between two chosen dates [duplicate]

This question already has answers here:
PHP: Return all dates between two dates in an array [duplicate]
(26 answers)
Closed 8 years ago.
I am building an app on php,mysql which is related to events. This is a specific feature where the user will repeat an activity/event . The user will select a start date and an end date . Now i am trying to figure out how to calculate the dates between those two selected dates. I am using jquery's datepicker for date selection .
$start_date = date("Y-m-d", strtotime($_POST['start_date']));
$end_date = date("Y-m-d", strtotime($_POST['end_date']));
Datediff doesn't seem to do the work as it only gives the difference between two dates . I want all the dates between those two dates .
Would be great if someone could advice as per how to go about this .
By this way You can fetch the dates between two dates
$start_date = strtotime($_POST['start_date']);
$end_date = strtotime($_POST['end_date']);
// Loop between timestamps, 24 hours at a time
for ( $i = $start_date ; $i <= $end_date ; $i = $i + 86400 ) {
echo date( 'Y-m-d', $i )."<br>";
}
function createDateRangeArray($strDateFrom,$strDateTo)
{
$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('Y-m-d',$iDateFrom)); // first entry
while ($iDateFrom<$iDateTo)
{
$iDateFrom+=86400; // add 24 hours
array_push($aryRange,date('Y-m-d',$iDateFrom));
}
}
return $aryRange;
}
Found the above solution and it works great. Thanks everyone for the time and advice.
I would recommend you to use DateTime objects
date_default_timezone_set("Europe/Istanbul");
$dt = new DateTime("10-01-2014");
$diff = $dt->diff(new Datetime("12-01-2014"));
for($i = 1; $i <= $diff->days; $i++) {
$nextdate = $dt->modify("+1 day");
echo $nextdate->format("d-m-Y"); echo "\n";
}

calculate the difference between two dates with strtotime [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 8 years ago.
I want to echo how much days are left between a variable and NOW. This is the code
$now = date('Y-m-d');
$daysleft= strtotime($starttheproject -$now);
echo
"Now =".$now.
"<br> Starttheproject =".$starttheproject.
"<br> Daysleft =".$daysleft;
Result of echo:
Row =2014-10-17
Starttheproject =2014-10-22
Daysleft =
Question:
How can calculate the number of days, so that the result will be '5'
I've been playing with code like this, with no luck:
$daysleft= date('Y-m-d', strtotime($starttheproject -$now));
Try with this:
$date = new DateTime('2014-03-10');
$finish = new DateTime();
$difference = $date->diff($finish);
$difference =$difference->format('%R%a');
if ($difference > 10) { //if the post is over 10 days old
echo 'Renew now';
}else{
echo 'Renew in '.$difference.' days</font>';
}

How to consider people whose age is minimum 18 years on today's date in PHP? [duplicate]

This question already has answers here:
Validate if age is over 18 years old
(7 answers)
Closed 8 years ago.
I've following code to check whether the user is born before today's date(the user date of birth is stored in a variable $form_data['dob']). The code for it is as below:
if(change_date_format_to_db($form_data['dob'], 'Y-m-d') > change_date_format_to_db(date('Y-m-d'), 'Y-m-d'))
$this->mValidator->push_error($errors_msgs['user_dob_greater'], 'dob');
function change_date_format_to_db( $date, $current_format, $seperator = "/" ) {
if ( empty($date) || empty($current_format) ) {
return "";
}
$current_format = strtoupper($current_format);
$format_array = explode("-", $current_format);
$date_values = explode($seperator, $date);
for ($i=0;$i<3;$i++) {
$date_data[ $format_array[$i] ] = $date_values[$i] ;
}
$mysql_date_format = $date_data["Y"]."-".$date_data["M"]."-".$date_data["D"];
return $mysql_date_format;
}
Now instead of checking the user's date before today's date I want the user should be minimum 18 years old on today's date. How should I do this with above code? Thanks in advance.
Keep it simple with DateTime Object
$birthday = DateTime::createFromFormat('Y/m/d', $form_data['dob']);
$diff = $birthday->diff(new DateTime());
// ^^ now
if ($diff->y < 18) {
echo 'this site requires +18yrs';
}

Categories