how to check two dates between from-date and to-date - php

I have two dates from-date & to-date.
I have to compare them from existing dates shown below, whether any of the day fall between them or not using php?
i can do for single date checking ,but i am confuse for the two date checking.
Example:
i have to check these dates:-> from=15 March 2013 & 15 April 2013 between the following dates whether any days falls in between these two date or not.
following data from db table
# from date to-date
-----------------------------------------
1 01 April 2013 30 April 2013 //here we will find as falling
2 01 May 2013 15 May 2013
3 01 June 2013 20 June 2013
Currently,in my mind not even a single logic is coming to try. Please give me any logic or suggestions regarding this issue..

The simplest way to compare dates is to convert them to a unix timestamp
Because the unix timestamp is an integer, you can simply use relational operators to compare them.
Example
// set some example data
$referenceDate = '01 April 2013';
$fromDate = '01 January 2013';
$toDate = '01 June 2013';
// convert dates to timestamps (strings to integers)
$referenceTimestamp = strtotime( $referenceDate );
$fromTimestamp = strtotime( $fromDate );
$toTimestamp = strtotime( $toDate );
// isBetween is Boolean TRUE if reference date is greater or equal fromDate and smaller or equal toDate
$isBetween = $referenceTimestamp >= $fromTimestamp and $referenceTimestamp <= $toTimestamp;
EDIT 1
To actually answer your question:
You have two ranges you need to test for overlap, this question has been answered here What's the most efficient way to test two integer ranges for overlap?
// our two ranges overlap if the following conditions are met
$dateRangeOverlaps = $referenceFromTimestamp <= $toTimestamp and $fromTimestamp <= $referenceToTimestamp;

Please try the code,
$ourdate = strtotime('1 April 2013'); // Your date which is to be checked
$from = strtotime('15 March 2013'); // From date
$to = strtotime('15 April 2013'); // To date
if ($ourdate >= $from && $ourdate <= $to)
{
echo "Date falls";
}
else
{
echo "No Date falls";
}
If you need to check several dates, pass it as an array, like below...
$i=0;
$dates= array("11 April 2013","16 April 2013");
foreach($dates as $ourdates)
{
$ourdate= strtotime($ourdates); //Your dates to be checked
$from = strtotime('15 March 2013'); // From date
$to = strtotime('15 April 2013'); // To date
if ($ourdate >= $from && $ourdate <= $to)
{
$i++;
}
}
if($i>0)
{
echo "Date falls";
}
else
{
echo "No Date falls";
}

Related

Dynamically change end date format based on current day

I am putting together a membership site that uses a pro rata system for memberships fees.
EG
$75.00 from 01 Apr YY - 31 Mar YY but if you join at say around Sep YY, then you only pay the pro rata of $38.00
My current coding is working fine - no probs.
But was wondering is there a way to to dynamically change the end date ($your_date) below so I don't have to manually edit it every 12 months?
So for instance, if today's date was 01 Feb 2017, then $your_date below is correct.
However, if todays date was 14 May 2017, then $your_date below of course is incorrect.
Membership year runs from 01 Apr this/last year - 31 Mar this/next year, so it would be
if Today's date <= 31 Mar this/next year && >= 01 Apr this/last year then do something..
<?php
$now = time(); // or your date as well
$your_date = strtotime("2017-03-31");
$datediff = $your_date - $now;
$SOS = floor($datediff/(60*60*24)*0.205);
$ORD = floor($datediff/(60*60*24)*0.115);
$GEN = floor($datediff/(60*60*24)*0.03);
?>
Or have I got a fixation with the code I'm using and there is something a lot simpler?
The PHP below is deliberately verbose in order that each line explains itself:
$Current_Year = date('Y');
$Current_Month = date('n');
$Current_Day = date('j');
$Next_Year = ($Current_Year + 1);
if ($Current_Month < 4) {
$Expiration_Year = $Current_Year;
}
if ($Current_Month > 3) {
$Expiration_Year = $Next_Year;
}
# Checks if date is March 31st
if (($Current_Month == 3) && ($Current_Day == 31)) {
$Expiration_Year = $Next_Year;
}
$Expiration_Date = $Expiration_Year.'-03-31';
It's a little complicated, but I think you want:
If the current date is April or later, then March 31 next year, otherwise it's March 31 this year.
Does that about sum it up? Of course, if they're applying on the 31st of March, that'd be a $0.00 fee...
So I think the code should be:
$your_year = ( date('m')>'3' ? ((int)date('Y')+1) : date('Y') )
$your_date = strtotime( $your_year . "-03-31" )

Daylight savings calculation in Php

In my application I have varchar field in mysql for 365 days in particular format i.e. '0000-04-12' (12th of April). Right now I am having a function that should check if particular date from field falls in daylight saving zone of Calgary-Canada, which ranges from 2:00 pm Second Sunday of March to 2:00 pm First Sunday of November. and returns true or false accordingly.
<?php
/*
#param $time form Database e.g. 0000-04-12
*/
function isDaylightSaving($time){
// to replace 0000 in start with current year
$today = substr_replace($time,date('Y'),0,4);
$date = new DateTime($today);
$week = $date->format('W');
// TO DO
// find date falls between 2:00 pm Second Sunday of March to 2:00 pm First Sunday of November.
return ;
}
?>
Right now its returning the week with respect to particular year but I need to check week with respect to month to perform particular logic, which I am unable to get.Thanks.
Try this:
if($time > date('Y-m-d',strtotime('second sunday of march')) &&
$time < date('Y-m-d',strtotime('first sunday of november'))){
return true;
}else{
return false;
}
strtotime() is a great function.
The example given by #Ezenhis sounds good, but lacks of some elements.
Extending it:
$low = new DateTime('#'.strtotime('second sunday of march'));
$high = new DateTime('#'.strtotime('first sunday of november'));
if($date > $low && $date < $high) {
// we're good
}
You shouldn't compare strings from date functions. Using DateTime is also a better way to handle dates/times in PHP since PHP 5.0.

Get Unix timestamp of specific date after another specific time

I can get the for example 19 March of specific date with this code:
$date = strtotime(" 19 March", $current_time);
For example if I gave the unix timestamp of 1st of January of 2010 as an input, It gave me 19 March of 2010. But also if I gave the unix timestamp of 20 March of 2010,I still get 19 March 2010. What I want is to get the next 19 March which in this case, It would be 19 March of 2011.
How can I do that?
Using PHP DateTime this can be achieved as follows:
// New DateTime object
$date = new DateTime('2010-03-19');
// Add a year
$date->add(new DateInterval('P1Y'));
// Output timestamp
echo $date->getTimestamp();
You can do something like as
$get = "19 March";
$given_date = "01 January 2010";
$date_month = date('d F',strtotime($given_date));
$year = date('Y',strtotime($given_date));
if(strtotime($given_date) - strtotime($date_month) < 0){
echo date('l,d F Y',strtotime("$get $year"));
}else{
echo date('l,d F Y',strtotime("$get ".($year+1)));
}
You should first get year from specified date. Then after you can create 19 march date with year and use strtotime() to get timestamp.
//add format according to your current_time variable format
$date = DateTime::createFromFormat("Y-m-d", $current_time);
echo $date->format("Y");
$fixed_date = strtotime($date->format("Y")."-03-19");
You can specify how many days or week you want to add or subtract from a day, as well as set the time with these functions
$nextUpdate = new DateTime("+5 day 1:00 pm");
echo $nextUpdate->getTimestamp();
$nextWeek = new DateTime("+1 week 9:00 am");
echo $nextWeek->getTimestamp();

php convert a date to a timestamp

I'm trying to convert 2 dates to timestamps. One is generated by php (today's date) and the other is input by the user
$today = date("d-m-y"); // 01-12-13
$start_date = "28-11-13";
$todaytimestamp = strtotime($today);
$starttimestamp = strtotime($start_date);
echo "$today > $start_date <br>$todaytimestamp > $starttimestamp";
Problem is that the result are incorrect
Result:
01-12-13 > 28-11-13 1008201600 > 1857686400
what's wrong ?
Always use four-digit years, as using two-digit years leads to endless headaches. 13 is not treated as a year; instead, 01-12-13 and 28-11-13 are interpreted as December 13, 2001 and November 13, 2028 by PHP. See PHP's Date Formats page, under "ISO8601 Notations": "Two digit year, month and day with dashes".
If you use 2013 instead, the issues disappear:
$today = date("d-m-Y"); // 01-12-2013
$start_date = "28-11-2013";
$todaytimestamp = strtotime($today);
$starttimestamp = strtotime($start_date);
echo "$today > $start_date <br>$todaytimestamp > $starttimestamp";
Output:
01-12-2013 > 28-11-2013
1385884800 > 1385625600
My understanding of the documentation is that you are trying to convert a readable string into a Unix based timestamp. When you attempt to convert 01-12-13 and 28-11-13 you will get undesired results because the strtotime function has no recollection of how to properly interpret these values. Something along the lines of the code below should help you out.
$today = date("jS M Y"); // 1st Dec 2013
$start_date = "28th Nov 2013";
$todaytimestamp = strtotime($today);
$starttimestamp = strtotime($start_date);
echo "$today > $start_date <br>$todaytimestamp > $starttimestamp";
output
1st Dec 2013 > 28th Nov 2013
1385877600 > 1385618400
Just adjust your code to fit within the limitations of the language and if these values are coming from a form field you should be able to quickly and easily convert them to a readable date.
The function assumes that you start with a year
(check : http://www.onlineconversion.com/unix_time.htm), change to
$start_date = "28-11-2013";
This way the function knows which is the year, it takes the default foramt which is (if not misstaken) Y-d-m

Format a date string in PHP

If I have a string which represents a date, like "2011/07/01" (which is 1st July 2011) , how would I output that in more readable forms, like:
1 July 2011
1 Jul 2011 (month as three letters)
And also, how could I make it intelligently show date ranges like "2011/07/01" to "2011/07/11" as
1 - 11 July 2001
(without repeating the 'July' and '2011' in this case)
You can convert your date to a timestamp using strtotime() and then use date() on that timestamp. On your example:
$date = date("j F Y", strtotime("2011/07/01")); // 1 July 2011
$date = date("j M Y", strtotime("2011/07/01")); // 1 Jul 2011
As NullUserException mentioned, you can use strtotime to convert the date strings to timestamps. You can output 'intelligent' ranges by using a different date format for the first date, determined by comparing the years, months and days:
$date1 = "2011/07/01";
$date2 = "2011/07/11";
$t1 = strtotime($date1);
$t2 = strtotime($date2);
// get date and time information from timestamps
$d1 = getdate($t1);
$d2 = getdate($t2);
// three possible formats for the first date
$long = "j F Y";
$medium = "j F";
$short = "j";
// decide which format to use
if ($d1["year"] != $d2["year"]) {
$first_format = $long;
} elseif ($d1["mon"] != $d2["mon"]) {
$first_format = $medium;
} else {
$first_format = $short;
}
printf("%s - %s\n", date($first_format, $t1), date($long, $t2));
As for the second one:
$time1 = time();
$time2 = $time1 + 345600; // 4 days
if( date("j",$time1) != date("j",$time2) && date("FY",$time1) == date("FY",$time2) ){
echo date("j",$time1)." - ".date("j F Y",$time2);
}
Can be seen in action here
Just make up more conditions
I would use strtotime AND strftime. Is a much simpler way of doing it.
By example, if a have a date string like "Oct 20 18:29:50 2001 GMT" and I want to get it in format day/month/year I could do:
$mystring = "Oct 20 18:29:50 2001 GMT";
printf("Original string: %s\n", $mystring);
$newstring = strftime("%d/%m/%Y", strtotime($mystring));
printf("Data in format day/month/year is: %s\n", $newstring);

Categories