This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to check if a date is in a given range?
How to check if date(entered by user) is in given range (Date format :-day month ie.:-1 june )
I am trying to find whether a date is in defined range. I'm using the following code:
$apple='25 March';
$udate= date('d F',strtotime($apple));
echo $udate;
$startDate='21 March';
$realStartDate= date('d F',strtotime($startDate)) ;
echo $realStartDate;
$endDate='19 April';
$realEndDate= date('d F',strtotime($endDate)) ;
if ($udate >= $realStartDate && $udate <= $realEndDate ) {
echo 'within tange';
}
else{
echo 'Not in range';
}
?>
Where am I going wrong?
try this one its working......
<?php
$udate = '25 March';
$udateTimestamp = strtotime($udate);
$startDate = '21 March';
$startDateTimestamp = strtotime($startDate);
$endDate = '19 April';
$eEndDateTimestamp = strtotime($endDate);
if ($udateTimestamp >= $startDateTimestamp && $udateTimestamp <= $eEndDateTimestamp)
{
echo 'within tange';
}
else
{
echo 'Not in range';
}
?>
Compare timestamps not the string representations!
if(strtotime($apple) < strtotime($endDate) && strtotime($apple) > strtotime($startDate)){
// All ok!
}
Like this
if(strtotime($givendate) > strtotime('3/21/xxxx') && strtotime($givendata) < strtotime('4/19/xxxx')) {
// Its within range
}
You can use DateTime
$userDate = new DateTime("2012-03-01");
if ( $userDate > new DateTime("2012-03-21 00:00:00") && $userDate < new DateTime("2012-04-19 23:59:59"))
{
// In Range
}
Putting it in a function if format is (1 july)
if (inRange ( "1 June", "3 March", "7 December" )) {
echo "In Range";
} else {
echo "Out Of Range";
}
function inRange($dateCheck, $dateFrom, $dateTo) {
$date = DateTime::createFromFormat ( "d F", $dateCheck );
$date1 = DateTime::createFromFormat ( "d F", $dateFrom );
$date2 = DateTime::createFromFormat ( "d F", $dateTo );
if ($date > $date1 && $date < $date2) {
return true;
}
return false;
}
try this
if (strtotime($udate) >= strtotime($realStartDate) && strtotime($udate) <= strtotime($realEndDate) ) {
echo 'within tange';
}
else{
echo 'Not in range';
}
Related
Does anyone know how to load current date data dynamically into a date in PHP ?
In example: the year, to automatically update.
I'm trying the following without success.
$nowDate = date('d/m/Y');
$cYear = date('Y');
$dateBegin = DateTime::createFromFormat('d/m/Y', '01/01/'.$cYear);
$dateEnd = DateTime::createFromFormat('d/m/Y', '31/12/'.$cYear);
if ($nowDate >= $dateBegin && $nowDate <= $dateEnd)
{
echo "is between";
} else {
echo 'OUT!';
}
You can compare DateTime objects between each-other:
$dateNow = new DateTime();
$dateBegin = new DateTime($dateNow->format('Y-01-01 00:00:00'));
$dateEnd = new DateTime($dateNow->format('Y-12-31 23:59:59'));
if ($dateBegin <= $dateNow && $dateNow <= $dateEnd) {
echo "is between";
} else {
echo 'OUT!';
}
demo
watch out data type
string date ( string $format [, int $timestamp ] )
public static DateTime DateTime::createFromFormat ( string $format , string $time [, DateTimeZone $timezone ] )
make the same type and compare.
$nowDate = date('d/m/Y');
$cYear = date('Y');
$dateBegin = DateTime::createFromFormat('d/m/Y', '01/01/'.$cYear);
$dateEnd = DateTime::createFromFormat('d/m/Y', '31/12/'.$cYear);
// change to DateTime type
$nowDate2 = DateTime::createFromFormat('d/m/Y', $nowDate) ;
echo $nowDate2->format('Y-m-d')."\n";
echo $dateBegin->format('Y-m-d')."\n";
echo $dateEnd->format('Y-m-d')."\n";
if ($nowDate2 >= $dateBegin && $nowDate2 <= $dateEnd)
{
echo "is between";
} else {
echo 'OUT!';
}
I have to check if the incoming date is between 3 and 6 months before today. If it is outside this range, it has to execute certain code.
below is the code
<?php
$date1 = '22-10-2017';
$date2 = date('d-m-Y' , strtotime('-3 months'));
$date3 = date('d-m-Y' , strtotime('-6 months'));
if((strtotime($date1) < strtotime($date2)) || (strtotime($date1) > strtotime($date3))){
echo "Inside Range";
}else echo "Out of Range";
?>
For example if
Incoming date is 20-02-2018 - Out of Range.
Incoming date is 20-10-2017 - Inside Range.
Incoming date is 20-08-2017 - Out of Range.
You are checking with || in your case you need to use && because you need date BETWEEN
$date1 = '20-08-2017';
$date2 = date('d-m-Y' , strtotime('-3 months'));
$date3 = date('d-m-Y' , strtotime('-6 months'));
if((strtotime($date1) <= strtotime($date2)) && (strtotime($date1) >= strtotime($date3))){
echo "Inside Range";
}else {
echo "Out of Range";
}
Explanation:
Need to change your condition from if((strtotime($date1) < strtotime($date2)) || (strtotime($date1) > strtotime($date3))) to if((strtotime($date1) <= strtotime($date2)) && (strtotime($date1) >= strtotime($date3))){
It's also significantly easier if you're using DateTime objects:
$date1 = new DateTime('20-08-2017');
$date2 = new DateTime('-3 months');
$date3 = new DateTime('-6 months');
if($date1 < $date2 && $date1 > $date3) {
echo "Inside Range";
} else {
echo "Out of Range";
}
You can do like this:
$today=date_create(date("Y-m-d"));
$date=date_create("2018-06-12");
$diff=date_diff($today,$date)->format("%a");
if ($diff > 90 && $diff < 180) {
echo "Inside range";
}
else {
echo "Out of range";
}
I have 2 dates like this YYYY-mm-dd and I would like to check if these 2 dates are a weekend.
I have this code but it only tests 1 date and I don't know how to adapt it; I need to add a $date_end.
$date = '2011-01-01';
$timestamp = strtotime($date);
$weekday= date("l", $timestamp );
$normalized_weekday = strtolower($weekday);
echo $normalized_weekday ;
if (($normalized_weekday == "saturday") || ($normalized_weekday == "sunday")) {
echo "true";
} else {
echo "false";
}
A couple of hints:
date('N') gives you normalised week day you can test against (no need to use localised strings)
Wrap it all in a custom function and you're done
You can use shorter code to check for weekend => date('N', strtotime($date)) >= 6.
So, to check for 2 dates — and not just 1 — use a function to keep your code simple and clean:
$date1 = '2011-01-01' ;
$date2 = '2017-05-26';
if ( check_if_weekend($date1) && check_if_weekend($date2) ) {
echo 'yes. both are weekends' ;
} else if ( check_if_weekend($date1) || check_if_weekend($date2) ) {
echo 'no. only one date is a weekend.' ;
} else {
echo 'no. neither are weekends.' ;
}
function check_if_weekend($date) {
return (date('N', strtotime($date)) >= 6);
}
Using your existing code, which is slightly longer, following is how you would check for 2 dates:
$date1 = '2011-01-01' ;
$date2 = '2017-05-27';
if ( check_if_weekend_long($date1) && check_if_weekend_long($date2) ) {
echo 'yes. both are weekends' ;
} else if ( check_if_weekend_long($date1) || check_if_weekend_long($date2) ) {
echo 'no. only one date is a weekend.' ;
} else {
echo 'no. neither are weekends.' ;
}
function check_if_weekend_long($date_str) {
$timestamp = strtotime($date_str);
$weekday= date("l", $timestamp );
$normalized_weekday = strtolower($weekday);
//echo $normalized_weekday ;
if (($normalized_weekday == "saturday") || ($normalized_weekday == "sunday")) {
return true;
} else {
return false;
}
}
Merging multiple answers into one and giving a bit extra, you'd come to the following:
function isWeekend($date) {
return (new DateTime($date))->format("N") > 5 ? true : false;
}
Or the long way:
function isWeekend($date) {
if ((new DateTime($date))->format("N") > 5) {
return true;
}
return false;
}
You can use the strtotime(); and date() functions to get the day of the date, and then check if is sat o sun
function check_if_weekend($date){
$timestamp = strtotime($date);
$day = date('D', $timestamp);
if(in_array($day, array('sun', 'sat'))
return true;
else return false;
}
I want to check whether the current year is greater than a date string(D-M-Y) here is my code
$OldDate = "09-30-2011";
$OldYear = strtok($OldDate, '-');
$NewYear = date("Y");
if ($OldYear < $NewYear) {
echo "Year is less than current year"
} else {
echo "Year is greater than current year";
}
You can use strtotime():
$OldDate = "2011-09-30";
$oldDateUnix = strtotime($OldDate);
if(date("Y", $oldDateUnix) < date("Y")) {
echo "Year is less than current year";
} else {
echo "Year is greater than current year";
}
UPDATE
Because you're using an unconventional datestamp, you have to use different methods, eg:
$OldDate = "09-30-2011";
list($month, $day, $year) = explode("-", $OldDate);
$oldDateUnix = strtotime($year . "-" . $month . "-" . $day);
if(date("Y", $oldDateUnix) < date("Y")) {
echo "Year is less than current year";
} else {
echo "Year is greater than current year";
}
NOTE: If you want to always be sure that your date gets correctly understood by strtotime, use YYYY-MM-DD
use the date function to get year
$OldDate = date("Y",strtotime("09-30-2011"));
$NewYear = date("Y",strtotime("now"));
if($OldYear<$NewYear)
{
echo "Year is less than current year"
}
else
{
echo "Year is greater than current year";
}
You can achieve your goal by doing:
$input_date = date("09-30-2011");
$input_date_arr = explode("-", $input_date);
$currYear = date("Y");
$inputYear = $input_date_arr[2];
if ($currYear > $inputYear) {
echo "Current year is greater than given year!";
} else {
echo "Current year is not greater than given year!";
}
$OldDate = "09-30-2011";
$OldYear = date('Y',strtotime($OldDate));
$NewYear = date("Y");
if($OldYear<$NewYear)
{
echo "Year is less than current year"
}
else
{
echo "Year is greater than current year";
}
You could convert the string to a timestamp, and check it with the current timestamp
if(time($OldDate) < time()){
// do stuff
} else {
// do other stuff
}
do this,
$dateString = '2021-02-24';
$yr = date("Y", strtotime($dateString));
$mon = date("m", strtotime($dateString));
$date = date("d", strtotime($dateString));
I would like to know how to check if the timestamp is today, tomorrow or the day after tomorrow.
I have e.g. :
$timestamp = "1313000474";
How to make a check if this timestamp is today,tomorrow or the day after tomorrow?
e.g.
if today then echo $output = "today";
if tomorrow then echo $output = "tomorrow";
if the day after tomorrow then echo $output = "dayaftertomorrow";
How to do this?
EDIT: corrected unix timestamp
Thank you in advance.
$timestamp = "1313000474";
$date = date('Y-m-d', $timestamp);
$today = date('Y-m-d');
$tomorrow = date('Y-m-d', strtotime('tomorrow'));
$day_after_tomorrow = date('Y-m-d', strtotime('tomorrow + 1 day'));
if ($date == $today) {
echo "today";
} else if ($date == $tomorrow) {
echo "tomorrow";
} else if ($date == $day_after_tomorrow) {
echo "dayaftertomorrow";
}
Keep your code clean...
$timestamp = "1313000474";
// Description demonstrate proposes only...
$compare_dates = array(
'today' => 'today!!',
'tomorrow' => 'Tomorrow!!!',
'tomorrow +1 day' => 'day after tomorrow? YEAH',
);
foreach($compare_dates => $compare_date => $compare_date_desc){
if(strtotime($compare_date) > $timestamp && $timestamp < strtotime($compare_date.' +1 day') ){
echo $compare_date_desc;
break;
}
}
EDIT: With this you dont have to worry if the timestamp is already without hours, minutes and seconds... Or create different output dates, replacing echo $compare_date_desc; by echo date($compare_date_desc,$timestamp);
<?php
$time = "20060713174545";
$date = date('Y-m-d', strtotime($time));
$now = date('Y-m-d');
$tomorrow = date('Y-m-d', time() + strtotime('tomorrow'));
$day_after_tomorrow = date('Y-m-d', time() + strtotime('tomorrow + 1 day'));
if ($date == $now){
echo "It's today";
}
elseif($date == $tomorrow){
echo "It's tomorrow";
}
elseif($date == $day_after_tomorrow){
echo "It's day after tomorrow";
}
else{
echo "None of previous if statements passed";
}
<?php
function getTheDay($date)
{
$curr_date=strtotime(date("Y-m-d H:i:s"));
$the_date=strtotime($date);
$diff=floor(($curr_date-$the_date)/(60*60*24));
switch($diff)
{
case 0:
return "Today";
break;
case 1:
return "Yesterday";
break;
default:
return $diff." Days ago";
}
}
?>