Automatically inactivate after a certain date - php

I want to make an advertisment manager. When an ad reaches its expiry date it becomes non-active. But when I tried to make it and tried it, all of the ads change into non-active even though they haven't reached their expiry dates.
Here's my code:
$query_banner = mysql_query("SELECT * FROM ad_tbl ORDER BY ID DESC LIMIT $from,$max_show") or die(mysql_error());
while($show=mysql_fetch_array($query_banner))
{
$no++;
if(($no%2)==0)
$color = '#f2f2f2';
else
$color = '#f9f9f9';
$expired_date = $show['expiry_date'];
$today_date = date("m/d/Y");
$expired = strtotime($expiry_date);
$today = strtotime($today_date);
if($expired > $today)
{
$valid = "yes";
}
else
{
$valid = "no";
$query_expired = mysql_query("UPDATE ad_tbl SET status='Non-Active' WHERE expiry_date <= $today") or die(mysql_error());
}
}

Try:
$expiredDateTime = new DateTime($expiry_date);
$expired = $expiredDateTime->format('U');
$today = date('U'); // This will default give you timestamp for "now", saving you a step
If that works then I fear your m/d/y format could be the root of the issue. To check you should echo your strtotime values out and stick them into a converter, see what it says.

Related

DateTime format question with negative numbers

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.

MySQL: How do I search by timestamp but convert timezone first?

I need to be able to get the total amount spent either monthly or daily. To do that I do sum(tblaccounts.amountin) but the issue i'm having is I need to convert my date column to a different timezone fist. I've tried messing with convert_tz(date,"+00:00","+10:00") but not sure how to use it with LIKE.
How can I get records for monthly or daily based on the $period variable while first converting the timestamp?
Something like WHERE convert_tz(date) LIKE $date_to_check%
function check_limit($period='monthlylimit') {
if ($period == 'monthlylimit') {
$date_to_check = date("Y-m");
}
else { ### Day period
$date_to_check = date("Y-m-d");
}
$select = "SELECT
sum(tblaccounts.amountin) as amountin
FROM tblaccounts
WHERE date LIKE '" . $date_to_check . "%'";
}
Utilizing the timezone change inside mysql, this should work out:
if ($period == 'monthlylimit') {
$date = date('Ym');
$format = '%Y%m';
} else {
$date = date('Ymd');
$format = '%Y%m%d';
}
$sql = "SELECT SUM(t.amountin) as amountin FROM tblaccounts t WHERE
DATE_FORMAT(CONVERT_TZ(t.date,'+00:00','+10:00'),'". $format ."') = '". $date ."'";
The correct timezone numbers, will depend on what you have mysql defaulted too, and your desired result. You may have to twiddle with that number.
Or you can change the timezone for the date on php's side and skip mysql tz converting:
$dt = new DateTime("now", new DateTimeZone($tz));// $tz to equal what you are aiming for
$dt->setTimestamp(time());// might not be needed
if ($period == 'monthlylimit') {
$date = $dt->format('Ym');
$format = '%Y%m';
} else {
$date = $dt->format('Ymd');
$format = '%Y%m%d';
}
$sql = "SELECT SUM(t.amountin) as amountin FROM tblaccounts t WHERE
DATE_FORMAT(t.date,'". $format ."') = '". $date ."'";
Note: I did not use LIKE here, as that is why I introduced DATE_FORMAT to set it to a single value to compare on.

How to get the nearest date match in PHP from a SQL Database

I am currently trying to get the nearest match of 2 sets of dates (d-m-Y) against a SQL database and then out put the matched InterestRate column name.
I have an example of my current code, this is mostly pseudo code and trying a few things out. If that helps.
//$XSS_BLOCK2 = "05-05-2016";
$XSS_BLOCK3 = "20-05-2016"; //By the way The '2016-05-20' is user input so it will not be '2016-05-20' all the time, so it could be anything '2014-08-15'.
$today = date('d-m-Y');
$interest = 0;
$securesqlstring = $secureconn->prepare("SELECT * FROM LatePaymentRates");
$securesqlstring->execute();
while($row=$securesqlstring->fetch())
{
echo $row['StartDate'];
echo $row['EndDate'];
echo $row['InterestRate'];
$varsin = array($XSS_BLOCK3, $today);
$DateRange = new DateTime($varsin);
$databasein = array($row['StartDate'], $row['EndDate']);
$DateRanges = new DateTime($databasein);
if(($DateRange >= $DateRanges) && ($DateRange >= $DateRanges)) {
$dayrate = $row['InterestRate'] * $XSS_BLOCK3 / 36500;
$start_date = new DateTime($DateRange);
$end_date = new DateTime($DateRanges);
$dd = date_diff($end_date, $start_date) * $dayrate;
$interest += $dayrate;
}
}
$LatePaymentInterest = $interest;
if (!$securesqlstring) // If there is an error it will show this message.
{exit("Error in the SQL");}
Do we really need to spell this one out? I feel like I must be missing something...
SELECT * FROM my_table WHERE '2016-05-20' BETWEEN startdate AND enddate;

comparing date and time php

i need to compare two dates where if one date is greater than the other then an sql will run. this is my code
date_default_timezone_set('Asia/Kuala_Lumpur');
$date = date('Y-m-d G:i:s');
$query = mysql_query("SELECT * FROM package_transaction");
if(mysql_num_rows($query) > 0) {
while($row = mysql_fetch_array($query)) {
$transac_code = $row['transac_code'];
$duedate = $row['payment_due'];
if(strtotime($date) > strtotime($duedate))
{
mysql_query("UPDATE package_transaction SET `status` = 'cancelled' WHERE `payment_due` = `$duedate` AND `transac_code` = `$transac_code`");
}
}
}
but its not working. please help
try this,
date("Y-m-d", strtotime($date)) > date("Y-m-d", strtotime($duedate))
could you try and use the date format before using strtotime
$duedate = $row['payment_due'];
$duedate = $duedate->format('Y-m-d G:i:s');

Php not reading date as <= to current date correctly

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.

Categories