PHP Date Handling - I promise I've read the documentation.=)
Moderator: General Moderators
PHP Date Handling - I promise I've read the documentation.=)
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
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
this is what i've come up with, i didn't calculate for the dates above year 2x.
let me know if it doesn't calc an age right.
Code: Select all
<?
$birthday = "01/04/1963";
$break = explode("/",$birthday);
if($breakї2] < 1970) {
$strip = 1970-$breakї2];
$breakї2] = 1970;
}
$date = mktime(0,0,0,date("m"),date("d"),date("Y"));
$bday = mktime(0,0,0,"$breakї0]","$breakї1]","$breakї2]");
echo intval(($date-$bday)/31536000+$strip);
?>My style
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.
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
I dont need the whole timestamp usually, so i use what i need...As for showing news from the last 2 days, say:
For displaying a common date:
Code: Select all
$getdate = getdate();
$dyday = $getdateї'yday'];
$dyear = $getdateї'year'];
$thedate = "$dyday,$dyear";
$getdate2 = getdate();
$dyday2 = $getdate2ї'yday'];
$dyear2 = $getdate2ї'year'];
if($dyday == "1") { $dyday2 = 365; } else { $dyday2 = $dyday2-1; }
$thedate2 = "$dyday2,$dyear2";Code: Select all
$today = getdate();
$month = $todayї'month'];
$mday = $todayї'mday'];
$year = $todayї'year'];
$date = "$month $mday, $year";IMHO i'd use date() over getdate(). just seems cleaner, and has more options for display.
overall i like to use the timestamp to calc. dates
Code: Select all
$getdate = getdate();
$dyday = $getdateї'yday'];
$dyear = $getdateї'year'];
$thedate = "$dyday,$dyear";
could be replaced with:
echo date("z,Y");
$getdate2 = getdate();
$dyday2 = $getdate2ї'yday'];
$dyear2 = $getdate2ї'year'];
if($dyday == "1") { $dyday2 = 365; } else { $dyday2 = $dyday2-1; }
$thedate2 = "$dyday2,$dyear2";
could be replaced with:
echo date("z,Y",time()-86400);- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Code: Select all
echo date('F d, Y');The date function is fairly versatile.
http://www.php.net/manual/en/function.date.php
Mac