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.
Related
I have a DATE field on my MySQL table. Let's say the value is 2015-05-05.
I would like to check if the current time is before or after that.
This is my code:
foreach ($row as $row)
{
$ExpDate = strtotime($row['exp_date']);
$Today = strtotime(date("Y-m-d"));
echo $Today . ' - ' . $ExpDate;
if ($Today > $ExpDate)
{
exit('<M>LicenseExpired<M>');
}
}
The problem is that it's not working. The value of exp without strtotime is 2015-05-05. It I add the strtotime, the value becomes an empty string.
How am I able to solve this problem or what would be a good way to compare dates in PHP?
When comparing dates in MySQL there is no reason to take the extra step and use string_to_time(). The below example should work just fine. The format of MySQL DATE is designed in such a way that comparisons of this nature work naturally without any extra steps needed.
foreach ($row as $row)
{
$ExpDate = $row['exp_date'];
$Today = date("Y-m-d");
echo $Today . ' - ' . $ExpDate;
if ($Today > $ExpDate)
{
exit('<M>LicenseExpired<M>');
}
}
try this
foreach ($row as $row)
{
$ExpDate = new DateTime($row['exp_date']);
$Today = new DateTime(date("Y-m-d"));
$interval = $ExpDate->diff($Today);
//echo $interval->format('%R%a days'); <--- to diffenernce by days.
if ($interval > 0)
{
exit('<M>LicenseExpired<M>');
}
}
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');
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.
I have a MySQL database with a date column.
I create a variable of the value in PHP like this:
$query = mysql_query ("SELECT customer_date FROM customers WHERE customer_id = {$_SESSION['session_id']}");
while ($result = mysql_fetch_object($query)) {
$date = $result->customer_date;
}
I need to convert the $datefrom YYYY-MM-DD to three variables
$yearvalue (for example 2012)
$monthname (for example August)
$dayvalue (for example 10)
And I need to be able to echo out anywhere in my code... How would I do this in a fancy way? I'm pretty new to coding...
Here:
while ($result = mysql_fetch_object($query)) {
$date = $result->customer_date;
$yearvalue = date("Y", strtotime($date) );
$monthname = date("F", strtotime($date) );
$dayvalue = date("d", strtotime($date) );
}
It'll only work/store the value of these values for the last row outputted from the $result.
how about this?
while ($result = mysql_fetch_object($query)) {
$date = $result->customer_date;
list($year,$month,$day,$hour,$minute,$second)=
explode('-',date('Y-F-d-h-i-s',strtotime($date)));
}
Also read this. You will need to play with Y-F-d-h-i-s as per your requirement.
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.