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!';
}
Related
I want to check a certain date that is stored in my DB, if this date is during this fiscal year it prints valid if it is not it prints invalid
here is my php code:
$init_date= date("2016/07/01");
$end_date= date("2017/06/30");
$i_date = strtotime($init_date);
$e_date = strtotime($end_date);
$date_db= strtotime($date);// this $date is retrieved from my DB
if ($e_date > $date_db && $i_date < $date_db)
{ echo "valid";}
else { echo "invalid";}
but the problem is that i don't want to set the start and the end dates manually, is there a way to make it dynamic? as it will be updated every year
This way your script will be a bit more dynamic.
// the below snippet checks the date retrieved from database
// against fiscal periods between years 2000 and 2050 and return the
// valid dates
$endYear=2000;
while($endYear<=2050) {
$end = $endYear.'/06/30';
$endDate = DateTime::createFromFormat('Y/m/d', $end);
$initDate = DateTime::createFromFormat('Y/m/d', $end);
$initDate = $initDate->sub(new DateInterval('P1Y'))->add(new DateInterval('P1D'));
$ddb = '2016-09-27';
$dateFrodDB = DateTime::createFromFormat('Y-m-d', $ddb);
if ($dateFrodDB>=$initDate && $dateFrodDB<=$endDate)
{ echo "valid\n";
echo "\tStartDate->\"".$initDate->format("Y-m-d")."\"\n";
echo "\tEndDate->\"".$endDate->format("Y-m-d")."\"\n";
echo "\tDateFromDatabase->\"".$dateFrodDB->format("Y-m-d")."\"\n";
}
$endYear++;
}
/* output
valid
StartDate->"2016-07-01"
EndDate->"2017-06-30"
DateFromDatabase->"2016-09-27"
*/
check this on PHP Sandbox
Try this,
<?php
$start_date = date('Y-m-d', strtotime(str_replace("/", "-", $initdate)));
$end_date = date('Y-m-d', strtotime(str_replace("/", "-", $end_date)));
$new_date = date('Y-m-d', strtotime(str_replace("/", "-", $date)));
if (($start_date < $new_date && $new_date < $end_date) || ($end_date < $new_date && $new_date < $start_date)) {
echo "valid";
} else {
echo 'invalid';
}
Here is working link
I have this code that says if days between 2 dates,
but i need it to work with Y-m instead of Y-m-d
how to achieve this?
example: i need to check if 2016-10 is between 2016-09 and 2016-12
my code:
$paymentDate = date('Y-m-d', strtotime($date));
$contractDateBegin = date('Y-m-d', strtotime($dfrom));
$contractDateEnd = date('Y-m-d', strtotime($dtom));
if (($paymentDate > $contractDateBegin) && ($paymentDate < $contractDateEnd)){
echo "is between";
}else{
echo "NO GO!";
}
Extract Month and Year and compare it.
$paymentDateY = date('Y', strtotime($date));
$paymentDateM = date('n', strtotime($date));
$contractDateBeginY = date('Y', strtotime($dfrom));
$contractDateBeginM = date('n', strtotime($dfrom));
$contractDateEndY = date('Y', strtotime($dtom));
$contractDateEndM = date('n', strtotime($dtom));
if ($paymentDateY >= $contractDateBeginY && $paymentDateY <= $contractDateEndY
&& $paymentDateM >= $contractDateBeginM && $paymentDateM <= $contractDateEndM){
echo "is between";
}
else
{
echo "NO GO!";
}
use Y-m is oK: Demo
$paymentDate=date('Y-m', strtotime($date));;
$contractDateBegin = date('Y-m', strtotime($dfrom));
$contractDateEnd = date('Y-m', strtotime($dtom));
if (($paymentDate > $contractDateBegin) && ($paymentDate < $contractDateEnd)){
echo "is between";
}else{
echo "NO GO!";
}
I have 2 input fields for birth dates. I would like to then calculate days until the next birthday for each individual and the difference between both dates entered to determine who is older. Is my syntax logical/where did i go wrong?
<form>
John's Birthday (YYYY-MM-DD):
<input type=text name=jhbd value=0><br> Jake's Birthday (YYYY-MM-DD):
<input type=text name=jkbd value=0><br>
<input type=submit>
</form>
$john_bd = $_POST['jhbd'];
$jake_bd = $_POST['jkbd'];
$today = date("Y/m/d");
$interval_jh = $john_bd ->diff($today);
$interval_jk = $jake_bd ->diff($today);
echo "There are".$interval_jh->days."days until John's birthday ";
echo "There are".$interval_jk->days."days until Jake's birthday ";
if ($john_bd > $jake_bd) {
echo "John is older";
} else if ($jake_bd > $john_bd) {
echo "Jake is older";
} else {
echo "Both Jake and John are twins!";
}
You need to convert them first.
$today = time();
$your_date = strtotime($john_bd);
$datediff = $now - $john_bd;
echo floor($datediff/(60*60*24));
it might help you
Your variant is almost correct. Use format for show diff between dates. Like in example
$origin = new DateTime('2009-10-11');
$target = new DateTime('2009-10-13');
$interval = $origin->diff($target);
echo $interval->format('%R%a days');
public function days_to_birth(string $date) : int {
if(empty($date))
{
return -1;
}
if($date == '0000-00-00')
{
return -1;
}
$ts = strtotime($date.' 00:00:00');
$bY = date('Y',$ts);
$bm = date('m',$ts);
$bd = date('d',$ts);
$nowY = date('Y');
$nowm = date('m');
$nowd = date('d');
if($bm == $nowm && $bd >= $nowd)
{
return $bd - $nowd;
}
if( ($bm == $nowm && $bd < $nowd) || ($bm < $nowm) )
{
$nextBirth = ($nowY+1).'-'.$bm.'-'.$bd;
$nextBirthTs = strtotime($nextBirth);
$diff = $nextBirthTs - time();
return floor($diff/(60*60*24));
}
if($bm > $nowm )
{
$nextBirth = $nowY.'-'.$bm.'-'.$bd.'00:00:00';
$diff = strtotime($nextBirth) - time();
return floor($diff/(60*60*24));
}
return -1;
}
Consider following code:
$today = date("d/m/Y");
$start_date = '20/11/2014';
$time1 = strtotime($start_date1);
$start_date1 = date('d/m/Y',$time1);
$end_date = '20/01/2015';
$time2 = strtotime($end_date1);
$end_date1 = date('d/m/Y',$time2);
if( $start_date1 <= $today && $end_date1 >= $today)
echo "yes";
else
echo 'no';
Even though $today is in between those start and end date, I get "no" in return. What might be the problem here? I just wanna check if today is in between those dates. Start date and end date are saved as string in DB.
Try this:
<?php
$now = new DateTime();
$startdate = new DateTime("2014-11-20");
$enddate = new DateTime("2015-01-20");
if($startdate <= $now && $now <= $enddate) {
echo "Yes";
}else{
echo "No";
}
?>
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';
}