The Little Guy Posted February 12, 2009 Share Posted February 12, 2009 How can I get this to format a date that is/was a long time ago "1879-03-14" (Albert Einstein's birth date) This: $aDate = '1879-03-14'; echo date('j\<\s\u\p\>S\<\/\s\u\p\> Y',strtotime($aDate)); doesn't output the correct date, how can I get it to output correctly? Here is what is outputted: December 31st 1969 Link to comment https://forums.phpfreaks.com/topic/144888-solved-old-dates/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 12, 2009 Share Posted February 12, 2009 Use the date/time object functions, because anything dependent on a Unix Timestamp (date, strtotime, mktime) won't work - <?php $datetime = date_create('1879-03-14'); echo date_format($datetime, 'F jS Y'); ?> Link to comment https://forums.phpfreaks.com/topic/144888-solved-old-dates/#findComment-760344 Share on other sites More sharing options...
The Little Guy Posted February 12, 2009 Author Share Posted February 12, 2009 I am also doing: $age = duration(strtotime(date('Y-m-d')) - strtotime(date($row['dob']))); echo $age['yr']; for my function duration to work properly, the value passed to it MUST be value in seconds, so what would I do for something like that? Link to comment https://forums.phpfreaks.com/topic/144888-solved-old-dates/#findComment-760345 Share on other sites More sharing options...
printf Posted February 12, 2009 Share Posted February 12, 2009 Just do it the database... (date, or datetime) // simple (just return the age)... stamp = birthday column name SELECT FLOOR((TO_DAYS(NOW()) - TO_DAYS(stamp)) / 365.25) AS age FROM table WHERE user = 1; // return 1967-05-03 12:45:54 => 41 // or you can get real fancy... stamp = birthday column name SELECT field1, CONCAT ( YEAR ( NOW() ) - YEAR ( stamp ) - IF ( DAYOFYEAR ( NOW() ) > DAYOFYEAR ( stamp ) ,0 ,1 ) , ' years ' , MONTH ( NOW() ) - MONTH ( stamp ) + IF ( DAYOFYEAR ( NOW() ) > DAYOFYEAR ( stamp ) ,0 ,12 ) , ' months and ' , IF ( DAY ( stamp ) >= DAY ( NOW() ) , DAY ( stamp ) - DAY ( NOW() ) , DAY ( NOW() ) - DAY ( stamp ) ) , ' days old' ) AS age FROM table WHERE user = 1; // return 1967-05-03 12:45:54 => 41 years 9 months 9 days old Link to comment https://forums.phpfreaks.com/topic/144888-solved-old-dates/#findComment-760391 Share on other sites More sharing options...
The Little Guy Posted February 12, 2009 Author Share Posted February 12, 2009 Thanks printf! I like the first example, that will work! How would you do an if statement (something I have never done before) to check how old someone was when they died, if they have a death date in the "dod" column? Link to comment https://forums.phpfreaks.com/topic/144888-solved-old-dates/#findComment-760757 Share on other sites More sharing options...
The Little Guy Posted February 12, 2009 Author Share Posted February 12, 2009 nvm, I will just do this: SELECT FLOOR((TO_DAYS(NOW()) - TO_DAYS(dob)) / 365.25) AS age, FLOOR((TO_DAYS(dod) - TO_DAYS(dob)) / 365.25) AS deathAge FROM people WHERE id = 1 If they haven't died it will just return null! Link to comment https://forums.phpfreaks.com/topic/144888-solved-old-dates/#findComment-760764 Share on other sites More sharing options...
printf Posted February 13, 2009 Share Posted February 13, 2009 I struggled with this one... Yuck! As much as people use MySQL, I could not find a good example that does accurate AGE lookups including (you are 20 years, 4 month and 17 days old). Not one example included LEAP YEAR into the mix. Really it drove me crazy for about 4 hours... Anyway here it is... The perfect AGE lookup that can go all the way back to the October 1582 cutover, the (Julian to the Gregorian calendar) switch... Hopefully it will help someone... (Definitely one to add to your snippets code library) stamp equals.. (change stamp to your datetime or date column) the column that holds the date or datetime you may want to find out how many years, months and days have passed since that date... SELECT (DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW()) - TO_DAYS(stamp)), '%Y')+0) As years, (DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW()) - TO_DAYS(stamp)), '%m')-1) AS months, CASE SIGN(DATE_FORMAT(NOW(), '%d') - DATE_FORMAT(stamp, '%d')) WHEN 0 THEN 0 WHEN -1 THEN (DATE_FORMAT(LAST_DAY(stamp), '%d') + DATE_FORMAT(NOW(), '%d') - DATE_FORMAT(stamp, '%d')) WHEN 1 THEN (DATE_FORMAT(NOW(), '%d') - DATE_FORMAT(stamp, '%d') - (DATE_FORMAT(NOW(), '%d') < DATE_FORMAT(stamp, '%d'))) END as days FROM table ORDER BY stamp; Link to comment https://forums.phpfreaks.com/topic/144888-solved-old-dates/#findComment-760966 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.