Page 1 of 1
date help?
Posted: Mon Mar 08, 2010 12:05 am
by adilmarwat2004
I have table in mysql and contains date(date) which is contains 2070-06-30.
When I provide the following statement in php
echo date('d-m-Y', strtotime($row['date']));
it gives 01-01-1970.
How I get my desire date of table field.
please help?
Adil
Re: date help?
Posted: Mon Mar 08, 2010 12:11 am
by jraede
Just to be clear, when you echo the value pulled from the database, is it "2070-06-30"? I'm pretty sure that format is not compatible with PHP's strtotime() function. You'd have to do a strreplace to replace '-' with '/', and then try strtotime on the replaced string. Additionally, I don't see where you are using the date function. Just echoing the strtotime would give you 0 if it cannot read the date properly; you'd have to use date() on the strtotime converted value in order to display the date you want.
For now, try switching the hyphens to slashes. If that doesn't work, let me know.
Re: date help?
Posted: Mon Mar 08, 2010 1:07 am
by pbs
Instead of using PHP strtotime use MYSQL function STR_TO_DATE(str,format), which will be easy.
Re: date help?
Posted: Mon Mar 08, 2010 4:33 am
by michaeru
Won't a simple date('Y-m-d') do it? The first parameter determines the format right?
DateTime class
Posted: Mon Mar 08, 2010 1:43 pm
by tr0gd0rr
If you have php 5.2 you can use the DateTime class:
Code: Select all
$date = new DateTime('2070-06-30');
echo $date->format('d-m-Y');
I recently did a project with Mortgage amortization dates. A 30 year mortgage puts you beyond the 2038 limit of `date()`. But DateTime can go
from -9999 to 9999. MySQL's range is
from 1000 to 9999
If you do not have php 5.2, I believe
Zend_Date can handle dates past 2038.