0

I'm am trying to take a UNIX timestamp and convert it into a string with PHP. This is easy given PHP's built in functions however I have a specific set of requirements which are causing me some confusion.

The logic should be as follows:

  • if timestamp is today - return today
  • if timstamp happened yesterday - return yesterday
  • if timestamp is older than yesterday but still this week return the day name ( Thursday, Tuesday etc.)
  • if timestamp happened last week - return last week
  • if timestamp is older than last week - return Month and Year ( formatted as June 2013, January 2011 etc.)

This appears quite simply from the outset but for some reason my brain is confusing itself over the matter.

Please Note: I do not want a solution based on pure arithmetic as the modern calendar system is quite complex and for accuracy I would like to use built in time functions which account for this. strtotime(), date() etc.

Thanks in advance

EDIT: The initial part of the logic that I am using so far is as follows. Of course this will only return today if the time stamp is today. I need to extend this to account for the other requirements. Perhaps using strtotime?

public function getTimeChange($timestamp)
{
            $today = date('Ymd');
            $date = date(Ymd', $timestamp);


            if($date == $today){
                return 'today';
            }
}
4
  • What have you got so far? Commented Jun 20, 2013 at 13:50
  • it sounds like you need to write a custom conversion function with a bunch of if statements to handle the special cases you mentioned. start there. Commented Jun 20, 2013 at 13:53
  • @sgroves Indeed it appears this is going to be the only way to do it... extending the logic I already have with numerous if/else statements Commented Jun 20, 2013 at 13:56
  • @gordyr yep, and there's nothing wrong with that. Commented Jun 20, 2013 at 14:03

2 Answers 2

1

Something like this:

$date = date_create("@{$timestamp}");

// date_create() == new DateTime()
if($date < date_create('last week')){
  return $date->format('M Y');

}elseif($date < date_create('this week')){
  return 'Last week';

}elseif($date < date_create('yesterday')){
  return $date->format('l');

}elseif($date < date_create('today')){
  return 'Yesterday';

}else{
  return 'Today or future date';
}

(DateTime objects can be compared)

But you shouldn't be using timestamps for dates anymore because they have limited range. Choose a standard date format and store them as strings...

Sign up to request clarification or add additional context in comments.

3 Comments

This appears to work perfectly as long as I change line 1 to use setTimestamp($theTimestamp); instead of createFromFormat. Many thanks! Could you explain why we should not be using UNIX timestamps anymore?
Just saw your edit... Thanks for that. It might be difficult now for this project but I will certainly bare that in mind in future. Many thanks :)
Just did :) timestamps only work for dates between 1970 and if I'm not mistaken - 2038.
0

... and for accuracy I will like to use built in time functions ...

At least the question "was it last week" depends on so much settings (First day in Week, ISO date or not, DST or not ...) that for accuracies sake you should better not rely on the built in PHP calendar functions.

I don't know who in the hell has set the timezones names to for instance "Amsterdam/Berlin" in PHP while there are ISO standards like CET etc. available. Dealing with this is actually p-i-t-a.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.