Page 1 of 1

PHP Date Handling - I promise I've read the documentation.=)

Posted: Thu May 09, 2002 2:17 pm
by GodOfHonk
Hello,

I'm currently working on a simple event calendar. I've been thinking about PHP and it's date handling functions.

First off, how is everybody handling dates before 1970 and after 2030-whatever? It would appear that all date manipulation is done with unix timestamps. For example, if I was born 01/04/1963 how would I be able to calculate my age?

Also, I was wondering how everybody prefered to store their dates in a database? Check out my poll.

Thanks!

GodOfHonk

Posted: Thu May 09, 2002 4:55 pm
by dusty
this is what i've come up with, i didn't calculate for the dates above year 2x.

Code: Select all

<?
$birthday = "01/04/1963";
$break = explode("/",$birthday);

if($break&#1111;2] < 1970) &#123;
  $strip = 1970-$break&#1111;2];
  $break&#1111;2] = 1970;
&#125;

$date = mktime(0,0,0,date("m"),date("d"),date("Y"));
$bday = mktime(0,0,0,"$break&#1111;0]","$break&#1111;1]","$break&#1111;2]");

echo intval(($date-$bday)/31536000+$strip);
?>
let me know if it doesn't calc an age right.

Posted: Fri May 10, 2002 5:48 am
by dethron
Same as the one of dusty. Can anyone find better :wink:
Thanx to dusty

My style

Posted: Fri May 10, 2002 9:59 am
by goghs
I like to use INT(10) to store time/date.
1. Portable
2. reasonable
Say you post some message at 2002-05-10 12:00:00, and your time zone is GMT. But for me (GMT+8, China), the right time for me is 2002-05-10 20:00:00. So the value stored in database as datetime is not reasonable for all, just for some. With timestamp stored (as INT), you can adjust to any user's timezone when displaying them.
3. Easier to calculate time difference
4. not prone to error
All date/time field type has problem sometimes if the value you store is not wellformed.

My date usage

Posted: Fri May 10, 2002 3:39 pm
by gotDNS
I dont need the whole timestamp usually, so i use what i need...As for showing news from the last 2 days, say:

Code: Select all

$getdate = getdate();
$dyday = $getdate&#1111;'yday'];
$dyear = $getdate&#1111;'year'];
$thedate = "$dyday,$dyear";

$getdate2 = getdate();
$dyday2 = $getdate2&#1111;'yday'];
$dyear2 = $getdate2&#1111;'year'];
if($dyday == "1") &#123; $dyday2 = 365; &#125; else &#123; $dyday2 = $dyday2-1; &#125;
$thedate2 = "$dyday2,$dyear2";
For displaying a common date:

Code: Select all

$today = getdate(); 
$month = $today&#1111;'month']; 
$mday = $today&#1111;'mday']; 
$year = $today&#1111;'year']; 
$date = "$month $mday, $year";

Posted: Fri May 10, 2002 9:54 pm
by dusty
IMHO i'd use date() over getdate(). just seems cleaner, and has more options for display.

Code: Select all

$getdate = getdate(); 
$dyday = $getdate&#1111;'yday']; 
$dyear = $getdate&#1111;'year']; 
$thedate = "$dyday,$dyear";

could be replaced with: 

echo date("z,Y");

$getdate2 = getdate(); 
$dyday2 = $getdate2&#1111;'yday']; 
$dyear2 = $getdate2&#1111;'year']; 
if($dyday == "1") &#123; $dyday2 = 365; &#125; else &#123; $dyday2 = $dyday2-1; &#125; 
$thedate2 = "$dyday2,$dyear2";

could be replaced with:

echo date("z,Y",time()-86400);
overall i like to use the timestamp to calc. dates

Posted: Sat May 11, 2002 10:24 am
by gotDNS
usefull! :D

Posted: Sat May 11, 2002 10:27 am
by gotDNS
wait, dusty, then it shows something like 130,2002 and 129,2002. I want it to shaw, say: May 11, 2002

Posted: Sat May 11, 2002 10:33 am
by twigletmac

Code: Select all

echo date('F d, Y');
Will output the date in the format Month Day, Year.

The date function is fairly versatile.
http://www.php.net/manual/en/function.date.php

Mac