date add weeks not working because VARCHAR ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

date add weeks not working because VARCHAR ?

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: date add weeks not working because VARCHAR ?

Post 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
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: date add weeks not working because VARCHAR ?

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: date add weeks not working because VARCHAR ?

Post 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.
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: date add weeks not working because VARCHAR ?

Post 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
Post Reply