[thelist] TIP: date functions in php & a one that I wrote myself

Jason Lustig lustig at acsu.buffalo.edu
Fri Jan 5 20:43:12 CST 2001


Well... I've been hanging around this list for quite a while, lurking and
reading all this great coding/design/whatever else stuff, and it's helped me
a lot. So, this is my little contribution back to the community.

<tip author="Jason Lustig" language="php" subject="php date function that I
wrote">

I wrote this function for a message board system taht I wrote in PHP, and
what it does is take a unix timestamp, and compare it to the current time.
If it is the same day, it only prints the time and whetehr it's AM or PM,
and if it's not the same day, it prints the calendar date (12/12, for
example) and if it is not the same year, it also prints the year to a
variable and returns it. Pretty nice for exporting nice formated dates for
timestamps.

function parsedate ($stamp) {
	$date = getdate();
	$year = $date['year'];
	$day=$date['mday'];
	$month=$date['mon'];
	$calendar = "$month/$day";

	$stampdate = getdate($stamp);
	$stampyear = $stampdate['year'];
	$stampminutes = $stampdate['minutes'];
	$stamphours = $stampdate['hours'];
	$stampday=$stampdate['mday'];
	$stampmonth=$stampdate['mon'];
	$stampcalendar = "$stampmonth/$stampday";

	$ampm = "AM";
	if (12 <= $stamphours)

		$stamphours = $stamphours-12;
		$ampm = "PM";
		}

	if ($stamphours == 0) $stamphours = 12;
	if ($stampminutes < 10)	$stampminutes = "0$stampminutes";

	$time = "$stamphours:$stampminutes $ampm";
	if ($stampyear != $year)
		$time .= " $stampcalendar $stampyear";
		elseif ($stampcalendar != $calendar)
			$time.= " $stampcalendar";

	return $time;
	}
</tip>

--Jason
http://www.alternate-universe.org/

*****

Your signiture determines your reality





More information about the thelist mailing list