I am having problems figuring out why the following code does not work. It returns the right numbers but when it hits -1 the page should stop.
The date fields are date in the mysql database.
$date = new DateTime(date("Y-m-d", strtotime($pay_posted1)));
$date->modify('+'.$time_frame1.' months');
$NEW_DATE = $date->format('Y-m-d');
$firstp = new DateTime(date("Y-m-d")); //CURRENT DATE
$secondp = new DateTime(date("Y-m-d", strtotime($NEW_DATE)));
$diffp = $firstp->diff($secondp);
$DIFFp = $diffp->format('%R%a');
$DIFF_p = $diffp->format('%a');
if ($DIFFp == +0) {
$PAYMENT_ERROR = "<center><h2><b><font color='#FF0000'>PAYMENT DUE
TODAY <a href=\"javascript:void(window.open('payment_history.php',
'',
'width=500,height=600,top=10,left=40,scrollbars=yes'))\">(View)
</a>
</font></b></h2></center>";
} elseif($DIFFp <= +10) {
$PAYMENT_ERROR = "<h2><b><font color='#FF0000'>PAYMENT DUE IN
$DIFFp DAY(S)</b></font></h2>";
} elseif ($DIFFp <= -1) {
$PAYMENT_ERROR = "<br><br><br><br><h1><b><font
color='#ff0000'>PAYMENT IS PAST DUE!! <br>
PLEASE FOLLOW THIS <a
href='http://wawoffice.net/contact.php'>LINK</a></font></h1>";
exit();
} else {
$PAYMENT_ERROR = "";
}
Thanks
-1 matches the condition elseif($DIFFp <= +10) so the last elseif will never be reached, you need to reverse the order of the elseif clauses.
Thank you gramamj42. That was it, have no idea how I missed that.
Related
i have the following code :
$now = date('2018-12-28 23:00:00');
$begintime = new DateTime('22:00');
$endtime = new DateTime('03:00');
if($now >= $endtime && $now <= $begintime){
// not between times
echo "Run";
} else {
// between times
echo "Skip";
}
and the echo is
Skip
because $now is bigger than $begintime that make the output is false
what is the correct way to know if time is not the between the $begintime and $endtime?
I searched all of the relevant issues and I've just wasted 3 days of my life because of this issues, but couldn't find something that even elucidated anything from this forum and google for me. Please help me this issue has already taken an absurd amount of days from my life already and sorry for my english before.. :D
Your code displays enormous amount of misunderstanding of how things are working and it leads to the problem. Please take a look at official documentation, you will see that date() returns string and DateTime is an object. They can't be compared directly, you need to convert them into comparable types beforehand. Also notice that DateTime expects to get a date, not just time. Actually without date being defined your $endtime is actually smaller then $starttime:
$begintime = new DateTime('22:00');
$endtime = new DateTime('03:00');
echo $begintime < $endtime ? 'Yes':'No';
This code snippet will return No.
You need to convert $now to a DateTime and you need to add dates to your start / end time marks. For example:
$now = \DateTime::createFromFormat('Y-m-d H:i:s', '2018-12-28 23:00:00', new \DateTimeZone('UTC'));
I can't provide example of converting start / end time marks because you have not defined how do they actually need to look like.
One solution may be :
$now = "23:00"; // date ('H:i');
$begintime = "22:00";
$endtime = "03:00";
$now_t = DateTime::createFromFormat('H:i', $now);
$begintime_t = DateTime::createFromFormat('H:i', $begintime);
$endtime_t = DateTime::createFromFormat('H:i', $endtime);
if ($now_t > $begintime_t || $now_t < $endtime_t)
{
echo "Skip";
}
else
{
echo "Run";
}
UPDATE
finnaly, i solve my problem..
can someone remove the duplicate tags from my question and find the correct title for this issue? maybe someone who have same issue like me, can search my question and find the answer..
sorry for my english, i still learning..
// this is variable from mysql database
$mysql_start = "2018-12-28 21:45:00"; // its from mysql DATETIME and time show when this script will be run
$begintime = "22:00"; // user can choose when this script stop
$endtime = "20:00"; // user can choose when this script run
$mysql_start = explode(' ', $mysql_start);
$taskdays = $mysql_start[0]; // 2018-12-28
echo $taskhours = $mysql_start[1]; // 21:45:00
echo "<br>";
$taskhours = explode(':',$taskhours);
$taskhours = $taskhours[0]; // 22
echo $begintime = date($begintime);
echo "<br>";
$begintime = explode(':',$begintime);
$begintime = $begintime[0]; // 20
echo "<br>";
echo $endtime = date($endtime);
echo "<br>";
$endtime = explode(':',$endtime);
$endtime = $endtime[0] - 1; // because if endtime is 6, so if its 05:59, the endtime will be end on 05
echo $endtime = str_pad($endtime, 2, '0', STR_PAD_LEFT); // if 6, it will add 0 so it will be 06
echo "<br>";
$jamarray = array("00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23");
$ray = array();
if ($begintime > $endtime){
echo '$begintime is bigger than $endtime<br>';
foreach($jamarray as $ray) {
if($ray >= $begintime or $ray <= $endtime){
//echo '<br>';
//print_r($ray);
$eray[] = $ray;
}
}
$aslinya = array_diff($jamarray,$eray);
print_r($aslinya);
if (in_array($taskhours, $aslinya))
{
echo " <= script run in this time";
}
}else{
echo '$begintime is less than $endtime<br>';
foreach($jamarray as $ray) {
if($ray >= $begintime and $ray <= $endtime){
//echo '<br>';
//print_r($ray);
$eray[] = $ray;
}
}
$aslinya = array_diff($jamarray,$eray);
print_r($aslinya);
if (in_array($taskhours, $aslinya))
{
echo " <= script run in this time";
}
}
how to understand if one date in php is less than another minus one day? I mean if for example a date is set to "2018/07/03"; how can I understand if a given date is less than "2018/07/02"
date1 : year1/month1/day1
date2: year2/month2/day2
<?php
if ($year1 >= $year2) {
if ($month1 >= $month2) {
if (($day1 - 1) > $day2) {
echo 'you could do something..';
}
}
}
?>
the above code fails if forexample $year2 = 2017 and $month2 = 11.. can anybody help me? thanks a lot..
Here, this should work.
$date_to_check = new DateTime($yesterday);
$today = new DateTime();
$time_diff = $today->diff($date_to_check)->d;
if($time_diff > 1) {
echo "This is greater than one day.";
}else{
echo "This is not greater than one day.";
$date = strtotime("2018/07/01");
$date2 = strtotime("2018/07/02");
if($date > $date2){
print('date is bigger');
// do stuff when date is bigger than date2
} else {
// else ...
print('date2 is bigger');
}
To convert string to date php has function named strtotime().
Compairing date objects is simple.
There is full information about strtotime()
http://php.net/manual/ru/function.strtotime.php
Another way:
$date = new DateTime("2018/07/01");
$date2 = new DateTime("2018/07/02");
if($date->modify("+1day") > $date2){
print('date is bigger');
// do stuff when date is bigger than date2
} else {
// else ...
print('date2 is bigger or equal');
}
Notice modify modifies $date object itself.
Read more here http://php.net/manual/en/class.datetime.php
I want to check if the date is more that or equals 2 days in the past only , But it also works if it's in the future.
Here is the code:
$d = new DateTime('04-06-2018');
$todayDate = new DateTime();
$interval = $d->diff($todayDate);
$days = $interval->format('%a');
if($days >= 2)
{echo 'true'; }
else
{echo 'false';}
It returns true whenever the difference between the two dates is more that or equals 2 , How to solve that?
You can do this by checking invert property in your DateInterval.
$d = new DateTime('3-04-2018');
$todayDate = new DateTime();
$interval = $d->diff($todayDate);
if($interval->format('%a') >= 2
&& 0 === $interval->invert
) {
echo 'true';
} else {
echo 'false';
}
Extend the if to check if date is in the past:
if($days >= 2 && strtotime($d) < time())
There is one more way to acheive this
return $interval->format("%r%a");
Cast to int if needed:
return (int)$interval->format("%r%a");
Here you will get positive and negative number to check future and past date.
Hope it helps
Just wanted to double check and make sure my line of thinking is correct. This hook runs every 48 hours, so I need to check if an event is happening today or tomorrow.
$now = date('Y/m/d');
$today = explode("/", $now);
The event start date has the same format, but the value varies, and is stored the same way.
if ( $today[1] == $eventDate[1] &&
(intval($today[2]) == (intval($eventDate[2]-1)) || (intval($today[2]) == intval($eventDate[2])-2))) {
//run code
}
In my opinion, you should consider to work with DateTime objects :
$today = new \DateTime();
$eventDateTime = \DateTime::createFromFormat('Y/m/d', $eventDate);
if ($today->format('d-m-Y') === $eventDateTime->format('d-m-Y')) {
...
}
If you want to check that an event is happening today or tomorrow, you could do like this :
$start = new \DateTime();
$start->setTime(0,0,0);
$end = new \DateTime();
$end->add(new \DateInterval('P1D'));
$end->setTime(23,59,59);
$eventDateTime = \DateTime::createFromFormat('Y/m/d', $eventDate);
if ($eventDateTime >= $start && $eventDateTime <= $end) {
...
}
This way you check if a date is in a certain period. Here I added 1 day to the current date but you can adapt the code and set more than 1 day if you want.
I have tried every combo I can think of / found and no matter what I do, my codet echos the message even if the account isn't locked out:
<?php
$infosql = "SELECT * FROM premiersounds_users WHERE customer_id = $id";
$inforesult = mysql_query($infosql) or die(mysql_error());
$info = mysql_fetch_array($inforesult);
//Get current date from server
$format="%m/%d/%y";
$c_date=strftime($format);
//set sessions
$_SESSION['current_date'] = $c_date;
//The date in the database is 10/31/11
$_SESSION['lockout_date'] = $l_date;
//Check is Current date = lockout date
if ($c_date <= $l_date) {
header("location:documnet_editors/edit_weddingplanner.php?id=$id");
}
else {
echo 'Whoops! Were sorry your account has been locked to edits
because your event is less than 48 hours from now or your event has passed.
To make changes to your event please contact your DJ.';
}
?>
<?php
//Destroy Session for Lockout Date to prevent bypasses
unset($_SESSION['lockout_date']);
?>
If your $l_date is populated, and I don't think it is, if it is stored as MM/DD/YY, you'll want to use PHP's strtotime to convert it to a unix timestamp for quick comparison:
if( strtotime($db_date) >= time() )
{
// do something
}
I would suggest comparing timestamps instead of formatted dates:
<?php
$date_a = new DateTime();
$date_b = new DateTime('2000-10-20 00:10:20');
if ($date_a->getTimestamp() > $date_b->getTimestamp()) {
echo 1;
} else {
echo 0;
}
convert your dates to unixtime for more accurate comparison. Add this function to your code:
function unix_time($date){
$unix_date = str_replace("-","/",$date);
$unix_date = str_replace(".","/",$unix_date);
$unix_date = str_replace(" pm","",$unix_date);
$unix_date = str_replace(" am","",$unix_date);
$time = strtotime($unix_date);
return $time;
}
then convert the dates to unix:
$l_date = unix_time($_SESSION['lockout_date']);
$c_date = unix_time($_SESSION['current_date']);
or you can also get the date directly from the database:
$l_date = unix_time($info['date_in_database']);
compare the dates in unix format:
if ($c_date = $l_date) {
// your code here
}
this should work.