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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply

How do you store your date/time values in your database.

Number - UNIX timestamp
9
43%
Date or DateTime field - 'YYYY-MM-DD', etc.
11
52%
Database Specific Timestamp
0
No votes
Other - please post!
1
5%
 
Total votes: 21

GodOfHonk
Forum Newbie
Posts: 6
Joined: Thu May 09, 2002 2:17 pm
Location: St. Louis

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

Post 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
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post 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.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Same as the one of dusty. Can anyone find better :wink:
Thanx to dusty
goghs
Forum Newbie
Posts: 1
Joined: Fri May 10, 2002 9:59 am

My style

Post 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.
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

My date usage

Post 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";
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post 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
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

usefull! :D
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

wait, dusty, then it shows something like 130,2002 and 129,2002. I want it to shaw, say: May 11, 2002
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply