I have a form that allows users to enter a date when they are available in the format dd/mm/yyyy.
What I want to be able to do is echo out each date between now (including today) and the date entered.
Could someone help me out? I'm not quite sure how to go about it.
For example, if someone was to enter 12/12/2011 and the date today was 10/12/2011 it would echo the following:
10/12/2011
11/12/2011
12/12/2011
Any help would be great.
$start_date = time (); // today
$end_date = strtotime ( .. your input date as string ... );
while ( $start_date + 86400 < $end_date ) {
echo date ('m/d/Y', $start_date );
$start_date += 86400; // full day
}
First of all you are using a DD-MM-YYYY, make sure your separators are - so that you don't have converting them to timestamps. Here is an example with the current format etc.
You should have more checks to make sure you do not get an infinite loop :).
$start_date = "10/12/2011";
$end_date = "12/12/2011";
print_r(dates_between($start_date, $end_date));
function dates_between($start_date, $end_date)
{
$date = array($start_date);
$start_date = str_replace('/', '-', $start_date);
$end_date = str_replace('/', '-', $end_date);
$current_date = $start_date;
while($current_date != $end_date)
{
$current_date = date("d-m-Y", strtotime("+1 day", strtotime($current_date)));
$date[] = str_replace('-', '/', $current_date);
}
return $date;
}
picked from here
<?php
$fromDate = ’01/01/2009′;
$toDate = ’01/10/2009′;
$dateMonthYearArr = array();
$fromDateTS = strtotime($fromDate);
$toDateTS = strtotime($toDate);
for ($currentDateTS = $fromDateTS; $currentDateTS <= $toDateTS; $currentDateTS += (60 * 60 * 24)) {
// use date() and $currentDateTS to format the dates in between
$currentDateStr = date(“Y-m-d”,$currentDateTS);
$dateMonthYearArr[] = $currentDateStr;
//print $currentDateStr.”<br />”;
}
echo “<pre>”;
print_r($dateMonthYearArr);
echo “</pre>”;
?>
//Assume dates are properly formatted.
$startDate;
$endDate;
while($startDate < $endDate){
$startDate = date("m/d/Y", strtotime("+1 day", strtotime($startDate)));
echo $startDate;
}
Related
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
for example I have two dates 2015-10-28 and 2015-12-31. from these I want to know how many saturday and sunday in that given date range. I can find the diff between that dates but I can't find how many weekends.
anyone ever made this?
here is my current code:
function createDateRange($maxDate, $cell, $lead, $offArray = array()){
$dates = [];
--$cell;
--$lead;
$edate = date('Y-m-d', strtotime($maxDate." -$lead day"));
$sdate = date('Y-m-d', strtotime($edate." -$cell day"));
$start = new DateTime($sdate);
$end = new DateTime($edate);
$end = $end->modify('+1 day');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start, $interval, $end);
foreach($period as $d){
$dt = $d->format('Y-m-d');
if(!in_array($dt, $dates)){
$dates[] = $dt;
}
}
return $dates;
}
basically I want to add sat+sun count to the date range.
The trick is to use an O(1)-type algorithm to solve this.
Given your starting date, move to the first Saturday. Call that from
Given your ending date, move back to the previous Friday. Call that to
Unless you have an edge case (where to is less than from), compute (to - from) * 2 / 7 as the number of weekend days, and add that to any weekend days passed over in steps (1) and (2).
This is how I do it in production, although generalised for arbitrary weekend days.
Use this function:
function getDateForSpecificDayBetweenDates($startDate, $endDate, $weekdayNumber)
{
$startDate = strtotime($startDate);
$endDate = strtotime($endDate);
$dateArr = array();
do
{
if(date("w", $startDate) != $weekdayNumber)
{
$startDate += (24 * 3600); // add 1 day
}
} while(date("w", $startDate) != $weekdayNumber);
while($startDate <= $endDate)
{
$dateArr[] = date('Y-m-d', $startDate);
$startDate += (7 * 24 * 3600); // add 7 days
}
return($dateArr);
}
The function call to get dates for all Sunday's in year 2015:
$dateArr = getDateForSpecificDayBetweenDates('2015-01-01', '2015-12-31', 0);
print "<pre>";
print_r($dateArr);
//周日0 周一1 .....
$data = 4;//周四
$t1 ='2015-10-28';
$t2 = '2015-12-31';
$datetime1 = date_create($t1);
$datetime2 = date_create($t2);
$interval = date_diff($datetime1, $datetime2);
$day = $interval->format('%a');
$result = ($day)/7;
$start = getdate(strtotime($t1))['wday'];
$end = getdate(strtotime($t2))['wday'];
if($data>=$start && $data<=$end){
echo floor($result)+1;
}else{
echo floor($result);
}
I want to get weekly dates on the basis of the start date and end date.
Suppose my start date is '2015-09-08' and end date is '2015-10-08'.
On the basis of these dates I want the following result using PHP. I want the weekly dates between the start date and end date.
2015-09-15
2015-09-22
2015-09-29
2015-10-06
you can take timestamps of both start date and end date and keep adding one weeks's timestamp to the current date until it is less than the end date timestamp.
something like below. check if this is what u asked for
$st=strtotime("2015-09-08");
$ed=strtotime("2015-10-08");
$wk=$st;
while($wk<$ed){
$wk = strtotime('+1 Week',$wk);
if($wk<$ed)
echo date("Y-m-d",$wk);
echo '<br>';
}
Try using:
select *
from table_name
where Column_name > '2015-09-08'
and Column_name < '2009-10-08'
OR
SELECT *
FROM Table_name
WHERE Column_name BETWEEN ‘2015-09-08’ AND ‘2015-10-08’
As you want weekly dates using php. The following code will do for you
<?php
$startdate='2015-09-08';
$enddate='2015-10-08';
$date=$startdate;
while($date<=$enddate)
{
$date = strtotime("+7 day", strtotime($date));
$date=date("Y-m-d", $date);
if($date<=$enddate)
echo $date."<br>";
}
?>
Php s strtotime function might come is handy here. You could try this:
$start = '2015-09-08';
$end = // you end date as string
$offset = strtotime($start);
$limit = strtotime($end);
for($t = $offset; $t < $limit; $t += 86400 * 7){
echo date('Y m d') ;
}
This link here has code to do exactly what you want, you can get the date of every Sunday or Monday or whatever day you choose between two dates
try this:
<?php
date_default_timezone_set('Asia/Kolkata');
$startdate= strtotime("15-09-08");
//$startdate= strtotime("08 September 2015");
$enddate= strtotime("15-10-08");
//$enddate= strtotime("08 October 2015");
$jump_date= $startdate;
if($enddate>$startdate)
while($jump_date< $enddate)
{
$jump_date= strtotime("+1 week", $jump_date);
if($jump_date< $enddate)
echo date('Y-m-d', $jump_date).'<br>';
}
?>
Using the built in php function strtotime to add a period of 1week
$ds='2015-09-08';
$df='2015-10-08';
$ts=strtotime( $ds );
$tf=strtotime( $df );
while( $ts <= $tf ){
$ts = strtotime('+1 week', $ts );
echo date( 'Y-m-d', $ts ).'<br />';
}
<?php
// Set timezone
//date_default_timezone_set('UTC');
// Start date
$date = '2015-09-08';
// End date
$end_date = '2015-10-08';
while (strtotime($date) <= strtotime($end_date)) {
echo $date."<br/>";
$date = date ("Y-m-d", strtotime("+7 day", strtotime($date)));
}
?>
I am using following codes to display start and end dates of current month.
function firstOfMonth() {
return date("m/d/Y", strtotime(date('m').'/01/'.date('Y').' 00:00:00'));
}
function lastOfMonth() {
return date("m/d/Y", strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00'))));
}
$date_start = firstOfMonth();
$date_end = lastOfMonth();
echo $date_start;
echo $date_end;
Question: How to get start and end dates of all months in a range of date given
For Eg:
function daterange($startdate,$enddate)
{
...
...
...
}
Expected result be array of start and end dates of each month between date range of $startdate and $enddate.
Help me how to do this....
<?php
//Function to return out start and end dates of all months in a date range given
function rent_range($start_date, $end_date)
{
$start_date = date("m/d/Y", strtotime($start_date));
$end_date = date("m/d/Y", strtotime($end_date));
$start = strtotime($start_date);
$end = strtotime($end_date);
$month = $start;
$months[] = date('Y-m', $start);
while($month < $end) {
$month = strtotime("+1 month", $month);
$months[] = date('Y-m', $month);
}
foreach($months as $mon)
{
$mon_arr = explode( "-", $mon);
$y = $mon_arr[0];
$m = $mon_arr[1];
$start_dates_arr[] = date("m/d/Y", strtotime($m.'/01/'.$y.' 00:00:00'));
$end_dates_arr[] = date("m/d/Y", strtotime('-1 minute', strtotime('+1 month',strtotime($m.'/01/'.$y.' 00:00:00'))));
}
//to remove first month in start date and add our start date as first date
array_shift($start_dates_arr);
array_pop($start_dates_arr);
array_unshift($start_dates_arr, $start_date);
//To remove last month in end date and add our end date as last date
array_pop($end_dates_arr);
array_pop($end_dates_arr);
array_push($end_dates_arr, $end_date);
$result['start_dates'] = $start_dates_arr;
$result['end_dates'] = $end_dates_arr;
return $result;
}
$start_date = '2011-07-29';
$end_date = '2012-03-31';
$res = rent_range($start_date, $end_date);
echo "<pre>";
print_r($res);
echo "</pre>";
?>
My Above Function will give the month range display of dates within a given range.
This function would be useful for monthly rent calculation as indian tradition.
It may help some one else....
Have a look at date function and scroll to format character t which gives you the number of days. Start date will always be 1 :-)
Doing this in a loop for the number of months between the two dates and storing the values in an array is up to you
function firstAndLast($d=''){
$d = $d?$d:time();
$f = mktime(0,0,0,date("n",$d),1,date("Y",$d));
$l = mktime(0,0,0,date("n",$d),date("t",$d),date("Y",$d));
return array($f,$l);
}
list($first,$last) = firstAndLast();
echo date("d/m/Y",$first)." ($first) - ".date("d/m/Y",$last)." ($last)";
You can pass a timestamp to the function or leave it blank and it will pick up the current time
How can I count the number of months from the following two dates below using the Procedural style method?
PHP code.
$delete_date = "2000-01-12 08:02:39";
$current_date = date('Y-m-d H:i:s'); //current date
You're looking for DateTime::diff?
$delete_date = "2000-01-12 08:02:39";
$date_format = 'Y-m-d H:i:s';
$current_date = date($date_format);
$diff = date_diff(date_create_from_format($date_format, $delete_date), date_create());
$months = $diff->m;
Something along the lines of that.
Using DateTime you will get the total months this way:
$d1 = new DateTime("2000-01-12 08:02:39");
$d2 = new DateTime();
$d3 = $d1->diff($d2);
$months = ($d3->y*12)+$d3->m;
You would still need to handle the leftover days $d3->d ... but that depends on your needs.
$delete_date = "2000-01-12 08:02:39";
$current_date = date('Y-m-d H:i:s'); //current date
$diff = strtotime($current_date) - strtotime($delete_date);
$months = floor(floatval($diff) / (60 * 60 * 24 * 365 / 12));
echo $months . "\n";
is this what you looking for?
$delete_date = "2000-01-12 08:02:39";
$current_date = date('Y-m-d H:i:s'); //current date
// convert date to int
$delete_date = strtotime($delete_date);
$current_date = strtotime($current_date);
// calculate it
$diff = $delete_date - $current_date;
// convert int to time
$conv_diff = date('format', $diff);
Try this, it is easy, maybe not enogh chick, but very effective.
function calculateMonthsBetweenDates($fMonth, $fDay, $fYear, $tMonth, $tDay, $tYear)
{
//Build datetime vars using month, day and year
$dateFrom = mktime(0, 0, 0, $fMonth, $fDay, $fYear);
$dateTo = mktime(0, 0, 0, $tMonth, $tDay, $tYear);
//Check dateTo is a later date than dateFrom.
if($dateFrom<=$dateTo){
$yearF = date("Y", $dateFrom);
$yearT = date("Y", $dateTo);
$monthF = date("m", $dateFrom);
$monthT = date("m", $dateTo);
//same year
if ($yearF == $yearT)
$months = ($monthT - $monthF);
else{
//different year
$months = (12*($yearT-$yearF)-$monthF) + $monthT;
}
return $months;
}
else
return false; //or -1
}