Forum Moderators: coopster

Message Too Old, No Replies

Working with dates in PHP

         

havoc

1:04 am on Jul 8, 2003 (gmt 0)

10+ Year Member



What is the best way to store dates in php? ..
At the moment i just store the time stamp

What i want to do is Have a list of so many things created on one day .. and then be able to pull that date out of the database ..

SO if i stored 10 things on the 8/july/2003 (time stamp 1057626025) how would i got about pulling only the ones i did today on the 8th july?

Thanks
karl

jatar_k

1:24 am on Jul 8, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



where are you storing them? in mysql?

havoc

1:31 am on Jul 8, 2003 (gmt 0)

10+ Year Member



yeh in mysql :)

jatar_k

1:46 am on Jul 8, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well, the first place you should look is at the storage medium itself

The DATETIME, DATE, and TIMESTAMP Types [mysql.com]

and given that the date format for mysql is YYYY-MM-DD then I don't really see any grand need to deviate from that. The following wil get todays date

$today = getdate() [ca.php.net];
$dayofweek = $today["weekday"];
$day = $today["mday"];
$month = $today["mon"];
$year = $today["year"];
if (strlen [ca.php.net]($month)) $month = "0" . $month;
if (strlen($day)) $day = "0" . $day;
$sqldate = $year,"-",$month,"-",$day;

that works for getting the server date for today

You could then do

$sql = "select * from tablename where datecolumn='$sqldate'";

etc..

For a set date you could construct the date based on input from drop downs or some type of date math (which is always fun ;)).

[edited by: jatar_k at 1:52 am (utc) on July 8, 2003]

havoc

1:52 am on Jul 8, 2003 (gmt 0)

10+ Year Member



Errr i am using unix time stamps not mysql time stamps ... i think theres a bit of difference :(

jatar_k

2:16 am on Jul 8, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well then we can do that too, I seem to not be paying attention ;)

I think I would still store the mysql format but that's just me.

So that would mean that you have to make a range and use a select between. Since the unix timestamp includes the time somewhere in there you can build two timestamps 00:00:01 on given date to 11:59:59 on the same date then use a

for july 8th

$start = mktime [ca.php.net](23,59,59,7,8,2003);
$end = mktime(0,0,1,7,8,2003);

$sql = "select * from tablename where datecolumn between $start and $end";

I think that is right, if you are not using time, just date, then you can set the first 3 params to 0 and select with just the date.

havoc

3:02 am on Jul 8, 2003 (gmt 0)

10+ Year Member



SWEET!
thanks