Page 1 of 1

date add weeks not working because VARCHAR ?

Posted: Thu Jan 16, 2014 6:44 am
by jonnyfortis
i am trying to add 14 weeks to a date that is echoed out. I think the problem is is the dates are stored in the database as VARCHAR

the date is stored as 08/01/2014 in the database ($row_dates['contractDate'])

<?php
$newDate = $row_dates['contractDate']

echo "<br /><font color=#ff0000>YOUR 2ND</font><br /><font color=#ff0000>PAYMENT IS DUE:<br />" . date('d-m-Y</font>', strtotime($newDate. ' + 14 weeks'));

?>

this is showing the result as

YOUR 2ND
PAYMENT IS DUE:
07/11/2014

but should be showing

9/4/2013

Re: date add weeks not working because VARCHAR ?

Posted: Thu Jan 16, 2014 7:39 am
by Celauran
Storing dates as DATE certainly makes more sense, but I think the problem is you're writing dates in an ambiguous format and PHP doesn't interpret it the same way you do.

Code: Select all

php > echo date('Y-m-d', strtotime('08/01/2014')) . "\n";
2014-08-01

Re: date add weeks not working because VARCHAR ?

Posted: Thu Jan 16, 2014 8:13 am
by jonnyfortis
Celauran wrote:Storing dates as DATE certainly makes more sense, but I think the problem is you're writing dates in an ambiguous format and PHP doesn't interpret it the same way you do.

Code: Select all

php > echo date('Y-m-d', strtotime('08/01/2014')) . "\n";
2014-08-01

thanks for the post

have you just changed the way the date is displayed?
it is the actual date that is incorrect.

thanks in advance

Re: date add weeks not working because VARCHAR ?

Posted: Thu Jan 16, 2014 8:32 am
by Celauran
PHP interprets dates with slashes to me dd/mm/yyyy and dates with dashes to be mm-dd-yyyy. The obvious solution is to use yyyy-mm-dd format to store your dates and use formatting rules to display however you please.

Re: date add weeks not working because VARCHAR ?

Posted: Mon Jan 20, 2014 5:05 am
by jonnyfortis
Celauran wrote:PHP interprets dates with slashes to me dd/mm/yyyy and dates with dashes to be mm-dd-yyyy. The obvious solution is to use yyyy-mm-dd format to store your dates and use formatting rules to display however you please.
thanks, that done, i changed the format d-m-Y in the database