Addition of time difference of time zone (to GMT value) [duplicate] - php

This question already has answers here:
Display time/date in specific timezone using date() function
(2 answers)
Closed 9 years ago.
The database stores values as YYYY-MM-DD HH:MM:SS (eg 2013-04-14%2015:00:00) which is in GMT time but the true value is the the above date and time + time zone difference of the server.
$time_heard = '2013-04-14%2015:00:00';
$time_diff = date('p')
$real_time = $time_heard + $time_diff ;
echo $real_time ;
How can i do this addition.
Thanks

If you're using MySQL you can use the CONVERT_TZ functionality straight in the SELECT.
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_convert-tz
If you need to do it in PHP, use the DateTime class by creating a new DateTime with the timezone set to GMT then changing the timezone and when you output the date it should display as the new timezone.

you can use date_default_timezone_set() function to set the time to the zone you want
here you can find different zones: http://www.php.net/manual/en/timezones.php
or if you insist, you can use date_parse_from_format()
here is the manual for it: http://php.net/manual/en/function.date-parse-from-format.php
edit: another way to do it with easy functions:
$tmstamp = strtotime($date1) + strtotime($date2);
echo date("YYYY-MM-DD HH:MM:SS", $tmstamp) ;

Related

Convert TIMESTAMP and timezone to a different timezone [duplicate]

This question already has answers here:
Timezone conversion in php
(9 answers)
Closed 6 years ago.
So I have in the DB a timestamp like this: 2016-03-31 21:10:15 this get's set in db to the server timezonedate_default_timezone_set("America/Los_Angeles");
Now when I list the data I need to convert this to a user timezone which I have stored somewhere else, is there a way to convert this and display it in a different timezone?
So if it was 5:00 PM for "America/Los_Angeles" I need to convert this to New_York for eg and show that time but for New_York timezone, I guess you know what I mean.
Use DateTime objects, and you can play around with timezones to your heart's content:
$dateTime = '2016-03-31 21:10:15';
$originalTimezone = 'America/Los_Angeles';
$newTimezone = 'America/New_York';
$dto = new DateTime($dateTime, new DateTimeZone($originalTimezone));
$dto->setTimezone(new DateTimeZone($newTimezone));
echo $dto->format('Y-m-d H:i:s');

PHP unix timestamp compare [duplicate]

This question already has answers here:
Does PHP time() return a GMT/UTC Timestamp?
(5 answers)
Closed 7 years ago.
I have an event date & time stored in my database that is being saved as a PHP unix timestamp. Im trying to check if today is greater than the event date and time.
The problem is - i'm trying to check it using local time (America/New_York), but im getting 2 different time zones. time() is displaying in EST and my database is displaying in UTC
Is there any way to check it correctly?
My Event Date from database:
1450447200 (December 18, 2015 2:00pm)
Im trying to compare it with php time()
am I maybe doing this wrong?
*UPDATE - ANSWER*
as per this answer I ended up doing this:
$utc_str = gmdate("M d Y H:i:s", time());
$today = strtotime($utc_str);
$event_datetime = $Event_timestamp;
date_default_timezone_set("America/New_York");
$utc_str_event = gmdate("M d Y H:i:s", $event_datetime);
$event_date = strtotime($utc_str_event);
if($today >= $event_date){
//Do Something
}
You probably have wrong date on server(s). time() always should return epoch (epoch = UTC).
btw. saving date as int on database it isn't best practice
I don't know if this is correct but have you tried using date_default_timezone_set

How to convert timestamp to actual Date and Time with GMT timezone [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have a timestamp which is like this
$timestamp = time();
so the $timestamp has 10 digit timestamp value...
now i use the follwoing code to convert it into (YYYY-mm-dd H:i) format by the following method
$act_time = date('Y-m-d H:i', $arr_timestamp);
But now i want to convert it based on the GMT timezone format..
How can i achieve that?
NOTE: I dont want to use php's DateTime object
You can use date_default_timezone_set():
date_default_timezone_set("GMT");
List of Supported Timezones.
You can save the current timezone before changing it:
$backupTimezone = date_default_timezone_get();
In addition, instead of using these you can change the default timezone in your ini file: date.timezone

How to find date differences in php [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP calculate person's current age
I want to find the persons age.
$time=$row['date_of_birth'];
$date=explode("-", $time);//gets the date birth from the database and puts it into an array
$a=getdate();//gets todays date
$diff_date= gregoriantojd($a["mon"],a["mday"],a["year"] ) - gregoriantojd(date[1],date[2],date[0]);//subtracts the differences between the dates, this is returned in seconds..
The question is whether this is the way to do it? and how do I convert the seconds and convert them to years (as an integer)?!
EDIT:
I think that the last line should be this:
return floor($diff_date /60*60*24*365);
Just try with strtotime (converts date string into timestamp):
$time = strtotime($row['date_of_birth']);
$age = date('Y', $time - time());
echo 'age: ' . $age;
If $time is supposed to be unix timestamp
$time=$row['date_of_birth'];
$date1 = new DateTime($time);
$date_diff = $date1->diff(new DateTime(), true);
$age = $date_diff->y;
I think you can make use of the date_diff function in PHP.
1st question: The question is whether this is the way to do it?
No. The function gregoriantojd (per the manual entry here) returns a value in days, not seconds. I like the date_diff conversion that Piotr gives the best.
2nd question: how do I convert the seconds and convert them to years
(as an integer)?!
Well, if you were starting with a number of seconds (which you aren't) then your second formula would be mostly correct, but of course it wouldn't account for leap-years. So for anyone older than 4, or at least 8, it would be off for one or more days around their birthday. Usually on their birthday is a pretty important time to get their age correct.

Convert unix timestamp [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to convert Unix timestamp to hhmmss?
Here is unix timestamp of today
1306702800
How to convert it to look like 30-05-2011 using php ?
You can use the date function to convert (and arbitrarily re-format) timestamps as such:
echo date('d-m-Y', 1306702800);
Additionally, you can get the current timestamp via the time function, so you could output the current date via: echo date('d-m-Y', time());.
Use PHP's date functions: http://nl3.php.net/manual/en/function.date.php
See date
echo date('d-m-Y', '%your-time-stamp%');

Categories