JeremyCanada26 Posted May 19, 2010 Share Posted May 19, 2010 if I have a number like -5 which represents the offset from UTC, how do I format a time that I already have in this format? 2010-04-30 11:38:56" btw, 2010-04-30 11:38:56 is already in UTC I'd preferably like to break it up into two parts afterwards. One that says "April 10th, 2010" and another that says "6:38 PM" (with no leading zero's) Quote Link to comment https://forums.phpfreaks.com/topic/202300-require-help-formatting-the-time/ Share on other sites More sharing options...
Sergey Popov Posted May 19, 2010 Share Posted May 19, 2010 Try this: $t = strtotime('2010-04-30 11:38:56'); $t = mktime(date('G',$t)-5,date('i',$t),date('s',$t),date('n',$t),date('j',$t),date('Y',$t)); $t1 = date('F jS, Y',$t); $t2 = date('h:i A',$t); echo $t1 , $t2; Quote Link to comment https://forums.phpfreaks.com/topic/202300-require-help-formatting-the-time/#findComment-1060783 Share on other sites More sharing options...
JeremyCanada26 Posted May 19, 2010 Author Share Posted May 19, 2010 Now that seems like it will work fine, but what happens if the -5 becomes -13 or +13, will this still actually work? Quote Link to comment https://forums.phpfreaks.com/topic/202300-require-help-formatting-the-time/#findComment-1060803 Share on other sites More sharing options...
Sergey Popov Posted May 19, 2010 Share Posted May 19, 2010 Yes, it will work if you replace in line #2 above: -5 to -13 or to whatsoever you like even -66. All you need to check this, is to surround the code above with <?php ?>, put it in a file with .php extension and try Quote Link to comment https://forums.phpfreaks.com/topic/202300-require-help-formatting-the-time/#findComment-1060812 Share on other sites More sharing options...
JeremyCanada26 Posted May 19, 2010 Author Share Posted May 19, 2010 what if -5 becomes a positive number like +5 or +10? Actually, no plus signs Quote Link to comment https://forums.phpfreaks.com/topic/202300-require-help-formatting-the-time/#findComment-1060813 Share on other sites More sharing options...
JAY6390 Posted May 19, 2010 Share Posted May 19, 2010 Wow that second line you used was very bloated Sergey, how about something like this $hour_diff = -4; $t = strtotime('2010-04-30 11:38:56'); $t = $t + ($hour_diff * 3600); $t1 = date('F jS, Y',$t); $t2 = date('h:i A',$t); echo $t1 , $t2; Quote Link to comment https://forums.phpfreaks.com/topic/202300-require-help-formatting-the-time/#findComment-1060839 Share on other sites More sharing options...
Sergey Popov Posted May 19, 2010 Share Posted May 19, 2010 Very smart.. The way you use will work in most cases but there will be problems with leap-year. My solution is more accurate Quote Link to comment https://forums.phpfreaks.com/topic/202300-require-help-formatting-the-time/#findComment-1060845 Share on other sites More sharing options...
JAY6390 Posted May 19, 2010 Share Posted May 19, 2010 Yours is in no way more accurate, mine adds the correct amount of seconds to the unix timestamp of the said time, and then formats it exactly like yours does. can you show me an example of how mine is different for any particular date? Quote Link to comment https://forums.phpfreaks.com/topic/202300-require-help-formatting-the-time/#findComment-1060847 Share on other sites More sharing options...
Sergey Popov Posted May 19, 2010 Share Posted May 19, 2010 You're right JAY6390, I mistake. I have a delusion that adding hours, days etc. with just incrementing by NNN seconds might be incorrect, while function mktime() respect things like daylight saving time and leap years. Anyway I always use mktime rather than multiplying seconds. Quote Link to comment https://forums.phpfreaks.com/topic/202300-require-help-formatting-the-time/#findComment-1060867 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.