PHP unix timestamp to UTC zend framework - php

Ok, this may be a simple question, maybe I am thinking about this all wrong. Who knows.
Anyway, I am using Zend Framework and I am attempting to use cookies for the first time in it. So from my understanding cookies in Zend use a format like:
Wednesday, 28-Feb-05 20:41:22 UTC
for the expires. So I am trying to figure out how to take time() and convert it to a similar date() variation of the above that is 2 hours in the future and UTC. But like I said I think I am over thinking it, so I need confirmation.
My thoughts is
$cookie_expire = time()+7200;
$cookie_expire = date('l, d-M-y H:i:s Z', $cookie_expire);
And I wanna say this is correct, however something in my head doesn't sit well with the notion so, hopefully someone can tell me if I am off on the idea or not.

http://php.net/manual/en/function.gmdate.php
See the comment by Glen # 4-Dec-2007 04:32:
This routine can help obtain a UTC timestamp:
<?php
$utc_str = gmdate("M d Y H:i:s", time());
$utc = strtotime($utc_str);
?>
Replace the time() with the timestamp you want to convert.

Related

Setting PHP DateTime doesn't appear to be observing TimeZones

I'm fairly new to PHP so forgive me if this is a stupid mistake that I haven't spotted
I've run into a problem where in our current system where we currently used strtotime and it was returning our date an hour ahead than it actually was set. E.g 1:15pm became 2:15pm when I set the timezone to be European rather than GMT.
I read that strotime had this problem but I can't get it to observe a different timezone if I try and set it.
So I tried working with PHPs DateTime instead.
The user enters the time and they select it as 1:15PM however we want to store it as 13:15. So I did this:
$t = DateTime::createFromFormat('h:i A', $venue['startTime']);
$t_24 = $t->format('H:i:s');
Then I try and create my Date object
$d = DateTime::createFromFormat('d-m-Y H:i:s', $venue['startDay'] . ' ' . $t_24);
$d->setTimezone(new DateTimeZone('America/New_York'));
echo ' ' . $d->getTimestamp();
Trying to set the timezone after the object is set because apparently it doesn't work if you add the timezone as the third argument in createFromFormat
My computers time is currently observing European time currently GMT+1 because we're observing daylight savings time in the UK, I select the time set on the through our system as 1:15pm and because I've set the timezone I expect the timestamp outputted equivalent to 7:15am as it's six hours behind European time, however when I convert the timestamp 1500639300 it's still equal to 2:15 PM. Probably done something stupid but can't quite figure out what? Any help would be appreciated :)
Timestamps have no time zone - they are always in UTC time. If you want to save timezone related data use another format! For example save in H:i:s, as you need it.
you can use gmdate() Function for this
<?php $current_time = gmdate('Y-m-d H:i:s'); ?>
http://php.net/manual/en/function.gmdate.php

Time format conversion

I need to convert a specific date format into local time (Europe/Sarajevo), I have the time in this format 2013-02-17T15:00:00Z which I don't really understand and this is why I don't know how to convert it to the Europe/Sarajevo time, who knows maybe it is already Sarajevo time, I don't know...
OK I can parse it and remove the T and Z and get a time but these letters mean something, probably they affect the result time...
The result time is for example 2013-02-17 18:00:00, probably there will be a difference due to the letters T and Z which are probably time offset.
Use DateTime. It's much better for working with timezones:
$datetime = new DateTime('2013-02-17T15:00:00Z');
$datetime->setTimeZone(new DateTimeZone('Europe/Sarajevo'));
echo $datetime->format('c');
Reference
DateTime
DateTimeZone
See it in action
You can use php date function like this
$date = '2013-02-17T15:00:00Z';
echo date('Y-m-d H:i:s',strtotime($date));
See the Manual

PHP: strtotime formatting

I am trying to put a readable time and date as part of a file name (in php). I am having all kinds of trouble with this and was hoping someone could help. I have tried several different recommendations that I have read around the internet (plus I read the manual) but I really haven't gotten anything to work right. Right now I have this:
$Time=strtotime("now");
$date=DateTime::createFromFormat('m/d/Y H:i:s', '7/24/2012 14:40:30');
$date_readable=$date->$Timestamp();
At that point I then add $date_readable to a file name. It compiles and runs but it doesn't format the date at all. It still gives it as a timestamp.
Any suggestions on how to make this work?
you can do it with simple date function for example
$time = strtotime("now");
$formatDate = date('F jS, Y h:i:s A', $time);
echo $formatDate;
this will print something like
July 25th, 2012 1:02:29 am
DateTime class is more powerful then using simple date function, as DateTime class offers powerful API's plus it is object oriented. however for simple date conversions i would stick to php's date function. as that could do my purpose.
for more formatting option have a look at this link http://www.php.net/manual/en/function.date.php#refsect1-function.date-parameters

How do I get a date in UK local time using PHP?

I'm using the unix timestamp to show when a message was posted in my project but when I came to displaying the exact time of the post I realized it was about 12 hours behind.
I'm in the UK and my server is in the US (might be the problem).
Is there a simple way of converting the unix timestamp into a readable British time?
$timestamp = time();
print date("F jS, Y", strtotime($timestamp));
Any help would be great,
Thanks!
At the top of your script, write:
date_default_timezone_set('Europe/London');
Or if your PHP is >= 5.2.0:
date_timezone_set('Europe/London');
Just call date_timezone_set with the appropriate parameter for the UK at the start of your script when displaying the dates (not when recording them; I 'm not sure, but it might result in the "wrong" timestamps being recorded).
Edit: The timezone you want is 'Europe/London'.
try date-default-timezone-set.
date_default_timezone_set('Europe/London');
Use date_default_timezone_set('Europe/London'); to set the time zone to London's time. Not sure if it works with summer/winter time.
the simplies way is to substruct gmt offset. e.g:
echo(date('Y-m-d h:i'), $myvalue - 60 * 60 * $nhours));
where $nhours - time defference in hours.
This one worked for me
date_default_timezone_set('Europe/London');

Could anyone pls convert this unixtime stamp in php?

I am struggling with this unixtimestamp and just cant find a correct format
Here is the stamp:
1295058844
And here is the result i want to achive:
01/14/2011 at 21:34 EST
And here is my almost correct but no luck code:
$start_unixtime = '1295058844';
date('m/d/Y \a\t H:i', intval($start_unixtime));
Basically i want EST time format, hope someone could help and sorry for such stupid question.
Thank you.
You need to use the date_default_timezone_set function before you call date.
date_default_timezone_set("America/New_York");
List of possible choices.
http://us2.php.net/manual/en/timezones.php
No need for intval.
date_default_timezone_set("TIMEZONENAME");
$start_unixtime = '1295058844';
echo date('m/d/Y \a\t H:i', $start_unixtime);
Maybe to get your "at" into there: date('m/d/Y',$x).' at '.date('H:i',$x)
After that I never could remember the codes, I tend to look them up every time.

Categories