How to compare EST date time with mysql data - php

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.

Related

How i can convert datetime to unix time stamp and compare it with other timestamp in php?

I have one page in that I have two textboxes where user will enter unix timestamp and datetime, I have to compare that both timestamp and datetime but in php the timezone giving different timestamp as per time zone so the code doesn't working in all places where time zones are different.
Please help on this I had used..
strtotime(),mktime(),getTimestamp() in php,
also used my sql for this..
SELECT UNIX_TIMESTAMP() but all are giving different time stamps.
$sql = "SELECT UNIX_TIMESTAMP('date time from text box') as timeStampfromdb";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$timeStampFromDateTime = $row["timeStampfromdb"];
}
if($timeStampFromDateTime == timestamp textbox value)
{
matched
}
else
{
not matched
}
You can set php timezone in php.ini file, for GMT you need "GMT".
You can also try in your code:
echo date('Y-m-d H:i:s T', time()) . "<br>\n";
date_default_timezone_set('GMT');
echo date('Y-m-d H:i:s T', time()) . "<br>\n";
time() should return the current unix timestamp.
If you are working with dates and times across time zones you should work with everything in GMT to avoid time zone issues. If you need to display a date and time at some point that is the place to do any time zone conversion that may be necessary.

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')";

PHP - mktime store wrong date in mysql

When I store the date through FROM_UNIXTIME() the date stores one day before.
My debug code:
$date = date("m-d-Y", time());
$date_unix = explode('-', $date);
if (count($date_unix) == 3) {
list ( $m, $d, $y ) = $date_unix;
$date_unix = mktime(0, 0, 0, $m, $d, $y);
}
echo "<br />Date: " . $date;
echo "<br />Date after mktime: " . $date_unix;
echo "<br />Date manual mktime: " . date("m-d-Y", mktime(0,0,0,8,18,2014));
I'm using the date from the server, change it to a unixtime with mktime() and trying to store in the database with FROM_UNIXTIME().
$conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
$sql = "INSERT INTO data ( data ) VALUES ( FROM_UNIXTIME(:date_unix) )";
$st = $conn->prepare($sql);
$st->bindValue(":date_unix", $date_unix, PDO::PARAM_INT);
$st->execute();
And after all this mess, the mysql still stores the date in the day before.
Ex: today is 08/18/2014 and in the database is 2014-08-17
The date_default_timezone in the server is "America/Cuiaba" and nothing change if I change the timezone.
Unix timestamps are UTC by definition. However, MySQL's FROM_UNIXTIME implicitly converts a unix timestamp into the MySQL server's timezone (which is a completely different setting than PHP's default_date_timezone setting).
The America/Cuiaba timezone is UTC - 4 hours, so the date "2014-08-18 00:00:00 (UTC)" is actually 2014-08-17 20:00:00 (America/Cuiaba)" -- thus the apparent 1-day difference.
Have a look at this answer on how to convert between timezones in your MySQL database.

MySQL CONVERT_TZ() funciton related issue

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";

Time Zones in PHP, Date, MySQL...?

I am trying to figure out how to display time in PST on my php site.
I have been putting in timestamps in the MySQL Database with NOW() and calling them like this:
date("m/d/y g:i a", strtotime($ref['lastupdated']))
However, the server time is in Central and However, as the website is geared towards people in the PST time zone. Basically it needs to show in PST. Is there a way I could do this - like, take that value and subtract 2 or something? Any ideas?
I'm not sure also if I need to go about this the PHP route or if the problem, rather, could be solved via MySQL.
Any ideas are appreciated - thanks!
$query = "SELECT refid, CONCAT(fname,' ',lname) refname, email, street, city, state, zip, interestlvl, status, added, lastupdated FROM referrals WHERE affid='$affid' ORDER BY added DESC;";
$result = mysql_query($query) or die("Query Failed: ".mysql_errno()." - ".mysql_error()."<BR>\n$Query<BR>\n");
if ($result) {
while ($ref = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '
<td bgcolor="#EFEFEF" class="list">'.
date_default_timezone_set('America/Chicago');
$date = new DateTime($ref['lastupdated']);
$date->setTimezone(new DateTimeZone('America/Los_Angeles'));
$date->format('m/d/y g:i a')
.'</td>';
}
}
$date = new DateTime($ref['lastupdated']);
$date->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo $date->format('m/d/y g:i a');
given that PST is America/Los Angeles. You have to make sure that date.timezone is set to the correct timezone for Central. This can either be accomplished via the php.ini or via date_default_timezone_set().
The code then treats your incoming string $ref['lastupdated'] as being in the default timezone (Central) and converts the date-time into the *America/Los_Angeles* timezone.

Categories