# Solved: PHP relative date format



## Xperience (Feb 13, 2007)

hi 

im developing forum software, called Xenon, currently in Alpha stages and i need some help with the relative date format function.

relative date is where it displays a string, instead of an actual date. for example, if today was the 7/7/07, then it would display "Today" instead of 7/7/07. 

im using a custom date function to support DST and varying timezones so if you need that function to help make the relative time function, then ill provide it.

thanks in advance.


----------



## MMJ (Oct 15, 2006)

Why don't you compare the two dates?


----------



## Xperience (Feb 13, 2007)

I dont understand what you mean there...?


----------



## jim4man (Aug 28, 2007)

all you need to do is compare the current date using the PHP function Date (d)."/".date(m)."/".date to your $date if true then display "today"

you could also then do things like todaysdate-1 = $date then display "Yesterday" todays date have to be the correct format obviously but you get the idea!!


----------



## Xperience (Feb 13, 2007)

How exactly would i go about doing that?


----------



## jim4man (Aug 28, 2007)

Xperience said:


> How exactly would i go about doing that?


ok i'll try write something at when i am work tomorrow for ya - remind me tho - mind like a well something forgetful


----------



## Xperience (Feb 13, 2007)

Ok  Thanks!

*This is a reminder... woo  *


----------



## jim4man (Aug 28, 2007)

$date = "31/08/07";
$today = date("d/m/y");
if($today == $date){print "today";}

that is the sort of thing to get your date to say today instead, yesterday is alittle more tricky - do u really want me to look into it?


----------



## jim4man (Aug 28, 2007)

```
$date = "30/8/2007";

$today  = mktime(0, 0, 0, date("m"),date("d"),date("Y"));
$today = getdate($today);

$yesterday  = mktime(0, 0, 0, date("m"),date("d")-1,date("Y"));
$yesterday = getdate($yesterday);
	
$t_str = $today['mday']."/".$today['mon']."/".$today['year'];
$y_str = $yesterday['mday']."/".$yesterday['mon']."/".$yesterday['year'];
if($t_str == $date){print "today<br>";}elseif($y_str == $date){print "yesterday<br>";}
```
thats what you want for the yesterday thing aswell - someone else might know a better way of doing it - but this works and i did it quickly


----------



## MMJ (Oct 15, 2006)

Heres for yesterday:


```
$date = "31/08/07";
$today = date("d/m/y");
$yesterday = date("d/m/y", time() - (60*60*24));
if($today == $date){print "today";}
```


----------



## Xperience (Feb 13, 2007)

ok cool. but how about things like "3 minutes ago", "1 hour ago" etc? Just like Invision Power Board does it.


----------



## jim4man (Aug 28, 2007)

then you need to use mktime http://us.php.net/mktime look here you use the same principal as the days one that i used, only -1 or -3 from the hour or minute argument of the mktime function.!


----------



## MMJ (Oct 15, 2006)

Xperience said:


> ok cool. but how about things like "3 minutes ago", "1 hour ago" etc? Just like Invision Power Board does it.


vB also has that. Look at their code to find out.


----------



## Xperience (Feb 13, 2007)

Dont have a copy of vb


----------



## Xperience (Feb 13, 2007)

jim4man said:


> then you need to use mktime http://us.php.net/mktime look here you use the same principal as the days one that i used, only -1 or -3 from the hour or minute argument of the mktime function.!


Reckon you could write one for me  Im a novice when it comes to dates :|


----------



## jim4man (Aug 28, 2007)

Xperience said:


> Reckon you could write one for me  Im a novice when it comes to dates :|


I'll see what I can do tonight - busy busy busy...


----------



## Xperience (Feb 13, 2007)

Ok, thanks alot for your persistence so far.


----------



## jim4man (Aug 28, 2007)

```
$year = 2007;
	$month = 8;
	$day = 4;
	$hour = 11;
	$min = 06;
	$sec = 0;

	$post = getdate(mktime($hour, $min, $sec, $month, $day, $year));
	$now  = getdate(mktime(date("H"), date("i"), date("s"), date("m"),date("d"),date("Y")));

	$result['seconds'] = $now['seconds'] - $post['seconds'];
	$result['minutes'] = $now['minutes'] - $post['minutes'];
	$result['hours'] = $now['hours'] - $post['hours'];
	$result['mday'] = $now['mday'] - $post['mday'];
	$result['wday'] = $now['wday'] - $post['wday']; 
	$result['mon'] = $now['mon'] - $post['mon'];
	$result['year'] = $now['year']  - $post['year'];
	$result['yday'] = $now['yday'] - $post['yday'];
	$result['weekday'] = $now['weekday'] - $post['weekday'];  
	$result['month'] = $now['month'] - $post['month'];
	$result['0'] = $now['0'] - $post['0'];

	print ($result['minutes']);
```
Rite this code will replcae the other code i sent, it calculates the time or day or month difference dynamically. The year month hour min sec and day vars are what you supply as in when the msg is posted. Then to the array $result holds all the differences, careful tho, if the day is yesterday if you print min it will not take into account the days difference so you need an if statement to check the largest value first ie year then month then day then hour then min. when there is a difference then stop and display it. I am assuming that you will be ok from here?

```

```


----------



## Xperience (Feb 13, 2007)

Ahh right. How exactly can i use this code? And, does it have to use mktime() or can i pass a normal date via date()?


----------



## MMJ (Oct 15, 2006)

In what format do you have the dates?

E.g. DD/MM/YYYY or MM-DD-YYYY?


----------



## Xperience (Feb 13, 2007)

DD MM YY

no slashes or dashes, instead, spaces.


----------



## jim4man (Aug 28, 2007)

Xperience said:


> Ahh right. How exactly can i use this code? And, does it have to use mktime() or can i pass a normal date via date()?


you need to make use of substr (http://uk.php.net/manual/en/function.substr.php) to seperate the day month year time.

unless MMJ is going to suggest something different


----------



## MMJ (Oct 15, 2006)

jim4man said:


> you need to make use of substr (http://uk.php.net/manual/en/function.substr.php) to seperate the day month year time.
> 
> unless MMJ is going to suggest something different


Yeah, he can explode it.


```
$date = "DD MM YY";
$date = explode(" ", $date);
$year = $date[2];
$month = $date[1];
$day = $date[0];
$hour = 11;
$min = 06;
$sec = 0;
```
@XP: How about the hours and minutes?


----------



## Xperience (Feb 13, 2007)

DD MM YY H:M 

Of course, the day and month will be shorthand format, like Wed, and Jan etc.


----------



## jim4man (Aug 28, 2007)

you should have enough to go on now then?


----------



## Xperience (Feb 13, 2007)

Yeh, i will when MMJ answers the thing about hours & min


----------



## MMJ (Oct 15, 2006)

Xperience said:


> Yeh, i will when MMJ answers the thing about hours & min


 What thing?


----------



## jim4man (Aug 28, 2007)

MMJ said:


> Yeah, he can explode it.
> 
> 
> ```
> ...


You can apply the same to the date and time it will just be $date[3] and $date[4]


----------



## Xperience (Feb 13, 2007)

Ok thanks


----------



## jim4man (Aug 28, 2007)

hang on i was wrong - that would be the case for DD MM YY H M

but if you using DD MM YY H:M then use substr to get the last two digits. or explode the string again on ":" so..


```
$date_time="DD MM YY HH:MM";
$tmp = explode(":", $date_tim);
$date= $tmp[0];
$min = $tmp[1];

$date = explode(" ", $date);
$year = $date[2];
$month = $date[1];
$day = $date[0];
$hour = $date[3];
```
I think that should do it - see what MMJ says


----------



## MMJ (Oct 15, 2006)

Try it out XP, see what you get.


----------



## Xperience (Feb 13, 2007)

YEh im stuck again lol. How is this thing meant to print the date to the page? I see no print() or echo() etc. Plus, how could i incorporate this into a function with params for hour minute etc?


----------



## jim4man (Aug 28, 2007)

you simply need to print $month for the month

as for the time the code is already there mind the spelling mistake tho!!
you have to do some thinking for your self!


----------



## Xperience (Feb 13, 2007)

Okie dokely


----------

