PHP/MySQL - Best way to work with dates in DD MMM YYYY format? -
i need display dates in dd mmm yyyy format on website i'm working on. first thought store dates in date type in mysql, convert proper format using php. however, returning 01 jan 1970 - not actual date of 04 may 1891.
i found few other places people have asked similar questions , answer select date_format(dt, '%d/%m/%y')
giving me 04/05/1891. want month 3 characters (may, jun, jul, aug, etc).
i thought storing dates varchar seems bad practice. looks have store date string , not able use php date() function because cannot handle dates prior 1970.
i may want things calculate ages sounds painful when dealing strings... wondering if has more elegant solution?
you can use date save dates in db.
date can store values '1000-01-01' '9999-12-31' (source)
php's old date functions (i.e. date(), strtotime()) on 32 bits , therefore work unix timestamps (they're limited 1970-2038 range).
with php 5's new class datetime can example:
$date = new datetime("1780-06-01"); echo $date->format('y-m-d h:i:s');
edit: compute difference between 2 dates (as stated in question), easy way is:
$datetime1 = new datetime('2009-10-11'); $datetime2 = new datetime('2009-10-13'); $interval = $datetime1->diff($datetime2); echo $interval->format('%y years');
Comments
Post a Comment