Destramic Posted April 18, 2007 Share Posted April 18, 2007 im trying to format a timestamp, for instance if date is today then it will return today then [time] and if date is yesterdays date then it will return it...I want it to work in different time zones, but im having a bit of trouble gettin it all together and working...can somone have a look and gove me some pointers? <?php timestamp_format($timestamp, $format = “d/m/Y G: i:s T”, $time_zone = “GMT”) { date_default_timezone_set($time_zone); $timestamp = expload(“ “, date($format, $timestamp)); $yesterdays_date = ; $todays_date = date($format, $timestamp[0]); if ($timestamp[0] == $todays_date) { $date = “Today at ” . $timestamp[1]; } else if () { $date = “Yesterday at ” .$timestamp[1]; } else { $date = $timestamp[0] . $timestamp[1]; } return $date; } ?> thanks ricky Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 18, 2007 Share Posted April 18, 2007 First, you're using "smart quotes" “ ”. They don't work with PHP. Use only normal single or double quotes. You didn't tell PHP that your function was a function. Here's a version of your code that works. I included three test cases at the bottom of the script: <?php function timestamp_format($timestamp, $format = 'd/m/Y G:i:s T', $time_zone = 'GMT') { date_default_timezone_set($time_zone); $today = date('Y-m-d'); $yesterday = date('Y-m-d',strtotime('yesterday')); $test_date = date('Y-m-d',$timestamp); $time_part = explode(' ',$format,2); switch (true) { case ($test_date == $today): $date = 'Today at ' . date($time_part[1],$timestamp); break; case ($test_date == $yesterday): $date = 'Yesterday at ' . date($time_part[1],$timestamp); break; default: $date = date($format,$timestamp); } return $date; } echo timestamp_format(time()).'<br>'; echo timestamp_format(strtotime('yesterday')).'<br>'; echo timestamp_format(strtotime('-2 days')); ?> Ken Quote Link to comment Share on other sites More sharing options...
otuatail Posted April 18, 2007 Share Posted April 18, 2007 I want it to work in different time zones don't understand this. If you are in UK and you get a hit from someone in australia, the time will still be the time on your UK server, but will probably say 4am. are you trying to get a time from another country? Desmond. Quote Link to comment Share on other sites More sharing options...
Destramic Posted April 18, 2007 Author Share Posted April 18, 2007 basically if $time_zone = "umt" or something different i want the time to show up in that time zone. Quote Link to comment Share on other sites More sharing options...
rcorlew Posted April 18, 2007 Share Posted April 18, 2007 You would have to use either a very complicated ip location script to get the timezone based on ip location or javascript to get the time on the users comuter. Since php exists only on the server, the time would have to come from a use of startoftime adjusted for the timezone differences on the users end. The only way to do that would be to make them set their timezone using sessions and then doing the startoftime adjustments from there. Or just use a central timezone and report that on the page like 12:00 am (GMT) I think that most people could figure it out. Quote Link to comment Share on other sites More sharing options...
Destramic Posted April 18, 2007 Author Share Posted April 18, 2007 i would change the time zone using this function date_default_timezone_set($time_zone); all the timezones would come from the database, i have a colum which tells me the users timezone and then i change the time zone accordingly Quote Link to comment Share on other sites More sharing options...
per1os Posted April 18, 2007 Share Posted April 18, 2007 // timezone is Mountain (-7) $time = time()+3600*-7; The time would than be displayed -7 hours. You have to do that with every time you want to display. You would need strictly the offset to accomplish this. Quote Link to comment Share on other sites More sharing options...
Destramic Posted April 18, 2007 Author Share Posted April 18, 2007 bah i thought this could simply be done via date_default_timezone_set($time_zone); have a page which could help me out? Quote Link to comment Share on other sites More sharing options...
rcorlew Posted April 19, 2007 Share Posted April 19, 2007 Could you just insert the time using a timestamp generated by php, then to make it easier for the users to understand how long ago that was to a time comparison to the current time. THe comparison would make it look something like this: Posted ----->4 hours and 30 minutes ago<---------- 1:43 pm (GMT) That may be the best and easiest way to do what you are wanting to do I think. Quote Link to comment 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.