MySQL CONVERT_TZ() funciton related issue - php

Below code is used to get last and next Saturday of current week
// GET Last Satuday of current week
$dt_week_start_time = strtotime("last saturday")+((20*3600)+ 1);
$dt_week_start_date = date('Y-m-d G:i:s', $dt_week_start_time);
// GET Last Satuday of current week
$dt_week_end_time = $dt_week_start_time + (7*3600*24) - 1;
$dt_week_end_date = date('Y-m-d G:i:s', $dt_week_end_time);
Below code is used to convert above both date related variables to EST timezone (default timezone is UTC)
$est_time = new DateTimeZone('EST');
$datetime = new DateTime($dt_week_start_date);
$datetime->setTimezone($est_time);
$dt_week_start_date = $datetime->format('Y-m-d G:i:s');
$datetime = new DateTime($dt_week_end_date);
$datetime->setTimezone($est_time);
$dt_week_end_date = $datetime->format('Y-m-d G:i:s');
Now I want to compare these EST timezone dates with dates in mySQL database table. Dates in mySQL database table are stored in UTC timezone by default. By studying on internet and someone suggested to use CONVERT_TZ function. I tried it but it is giving error like mentioned below
What is wrong in my query? Please advise.
Below 2 queries giving this error: 'Notice: Undefined index: purchasedatetime ...'
$str_query_select = "SELECT CONVERT_TZ(purchasedatetime, 'UTC', 'EST' ) FROM t_product_purchase WHERE sellerpkid=1";
$str_query_select = "SELECT CONVERT_TZ((SELECT purchasedatetime FROM t_product_purchase),'UTC','EST') FROM t_product_purchase WHERE sellerpkid=1";

Related

Different dateTime while converting into UTC in PHP 8.1?

I am trying to convert date time into UTC, on my previous project it was working and giving the correct DateTime as per requirement but when I run the same code on PHP 8.1 I got a different time!
Previous working code (PHP 5.6)-
function convertDateWithTimeZone($src_tz, $dest_tz, $src_date, $date_format = "Y-m-d H:i:s")
{
$dest_date = "";
$curr_tz = date_default_timezone_get();
date_default_timezone_set($src_tz) or die("Can not change the default time zone");
$strtotime = strtotime($src_date);
date_default_timezone_set($dest_tz) or die("Can not change the default time zone");
$dest_date = date($date_format, $strtotime);
date_default_timezone_set($curr_tz) or die("Can not change the default time zone");
return $dest_date;
}
$startdate = "2023-02-11 00:00:00";
$enddate = "2023-02-14 23:59:59";
$fromdate = convertDateWithTimeZone("Asia/Kolkata", "UTC", $startdate, $date_format = "Y-m-d H:i:s");
$todate = convertDateWithTimeZone("Asia/Kolkata", "UTC", $enddate, $date_format = "Y-m-d H:i:s");
I got the result for strtotime($fromdate) is 1676034000.
Now, when I run the same code in PHP 8 I got a different result -
I got the result for strtotime($fromdate) is 1676053800.

Date subtraction in php

I have the following dates that I get from database as well as from php date. I did some formatting to the date from database to suit accordingly. Those are as follows
From database
$startDate = $tagQueryRows['startDate'];
$startDate = strtotime($startDate);
$startDate = date( 'd-m-Y', $startDate );
Take note that the startDate in database is in this format 2019-09-02 10:26:44
From php date
$todayDate = date('d-m-Y');
Then I did a subtraction as follows
$totalDaysCompleted = ($todayDate- $startDate);
When I did this, it shows the correct number of days but with warning
Notice: A non well formed numeric value encountered
Thus I edited my code as below
$totalDaysCompleted = (strtotime(str_replace('-','/',$todayDate)) - strtotime(str_replace('-','/',$startDate)));
This time the warning gone, but it is not showing the $totalDaysCompleted correctly. The $totalDaysCompleted should be a number like 1, 2, 3 etc. But now it is showing weird number such as -150000 etc.
Can someone help me?
After some trial and errors, I got it right by doing the following way
$startDate = $tagQueryRows['startDate'];
$startDate = strtotime($startDate);
$todayDate = date('d-m-Y');
$totalDaysCompleted = (strtotime($todayDate)) - $startDate;
$totalDaysCompleted = round($totalDaysCompleted/(60*60*24),0);

How to check if date is today's date with PHP

Say, I have 4 dates and the date today is 1/7/16:
1/7/16 1:06:02
1/7/16 8:01:24
1/8/16 7:02:23
1/6/16 3:12:34
How can I only pick 1/7/16 1:06:02 and 1/7/16 8:01:24.
What date function from PHP can I use to only get the date of today?
Thanks!
UPDATE:
This is being used as a MYSQL selector.
Example: $db->query("DELETE * FROM entry WHERE date='$today'");
How would this work? How can I get my MYSQL query to select only the dates for today?
UPDATE 2:
I've tried using curdate(), but the code is not working...
This is what I'm doing:
$db->query("DELETE * FROM entry WHERE date=curdate()");
What am I doing wrong?
LAST UPDATE: curdate() worked properly...
UPDATE: As MySQL query, you can do this as follows:
$db->query("DELETE * FROM entry WHERE DATE(date) = CURDATE()");
CURDATE() returns today's date.
If you want to do with php, see below to use the DateTime class.
$now = new DateTime;
$otherDate = new DateTime('2016-01-01'); // or e.g. 2016-01-01 21:00:02
// Setting the time to 0 will ensure the difference is measured only in days
$now->setTime( 0, 0, 0 );
$otherDate->setTime( 0, 0, 0 );
var_dump($now->diff($otherDate)->days === 0); // Today
var_dump($now->diff($otherDate)->days === -1); // Yesterday
var_dump($now->diff($otherDate)->days === 1); // Tomorrow
The answer by schellingerht is totally correct, but
$now = new DateTime;
should be this instead:
$now = new DateTime('Today');
That will ensure that the date from today with 0 hours and 0 minutes is selected. Otherwise the output could be Today even if the date is tomorrow.
Hope that helps!
Convert to timestamp and then format for only the date. Then compare to the same format of todays date:
var_dump(
date('Ymd', strtotime('1/7/16 1:06:02')) === date('Ymd')
);
Try using DATE() (http://www.w3schools.com/sql/func_date.asp). You don't need to convert your dates to a string - this is not efficient.
DELETE FROM yourTable WHERE DATE(yourDateField) = DATE(NOW)
You could compare today's date with Y-m-d format, with your date parsed with the same format.
$today = date('Y-m-d');
$otherdate = date('Y-m-d', strtotime('2022-02-02 16:00:26'));
if($today === $otherdate){
\\ your code for Today
} else {
\\ your code for not Today
}

Time stored in DB minus 2 weeks

I would like to show the date 2 weeks before the date stored in a DB
The date isnt stored in Timestamp, it is stored like 01/01/2015
I have tried the below but this isnt working, can anyone help?
echo date('$valid_to', strtotime("-2 week"));
I would use DateTime class instead.
// timezone is optional
$date = new DateTime($valid_to, new DateTimeZone('Europe/Vilnius'));
echo $date->modify('-2 weeks');
// there you have your wanted date
$valid_date = $date->format('Y-m-d');
Then would recommend STR_TO_DATE mysql function to convert to correct timestamp.
For example:
$query = "SELECT * FROM table WHERE time_col <= STR_TO_DATE('" . $valid_date . "', '%Y-%m-%d')";

How to compare EST date time with mysql data

I have purchase data in mysql database table. All data on server are stored in UTC timezone.
I can convert date-time in EST timezone using below code.
$date = date_create(date('Y-m-d H:i:s'), timezone_open('Etc/GMT+0'));
date_timezone_set($date, timezone_open('Etc/GMT+5'));
I am getting last and next Saturday of current day using below code.
$dt_week_start_time = strtotime("last saturday")+((20*3600)+ 1);
$dt_week_start_date = date('Y-m-d G:i:s', $dt_week_start_time);
$dt_week_end_time = $dt_week_start_time + (7*3600*24) - 1;
$dt_week_end_date = date('Y-m-d G:i:s', $dt_week_end_time);
But how can i compare above EST timezone converted date-time with mysql data stored in UTC timezone? I need to convert data of 'purchasedatetime' field in EST timezone when I fire below query. Is this possible? Or am I doing it in wrong way? Please advise.
$str_query_select = "SELECT *, SUM(extendedprice) AS gross_price FROM t_product_purchase ";
$str_query_select .= " WHERE purchasedatetime BETWEEN '".$dt_week_start_date ."' AND '".$dt_week_end_date."'";
$str_query_select .= " AND sellerpkid=1";
$str_query_select .= " GROUP BY purchasedatetime ORDER BY purchasedatetime DESC ";
You can see this:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_convert-tz
or
SET time_zone = 'proper timezone';
being done once right after connect to database. and after this all timestamps will be converted automatically when selecting them.
in php you can do this:
$date = new DateTime($dt_week_start_date);
$date->setTimezone(new DateTimeZone('Europe/Moscow')); // based on your required zone
echo $date->format('Y-m-d H:i:s');
then use it in your sql.

Categories