Jump to content

[SOLVED] Old Dates


The Little Guy

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.