Jump to content

timestamp formats


Destramic

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.