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
date add weeks not working because VARCHAR ?
Moderator: General Moderators
-
jonnyfortis
- Forum Contributor
- Posts: 462
- Joined: Tue Jan 10, 2012 6:05 am
Re: date add weeks not working because VARCHAR ?
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 ?
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 ?
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 ?
thanks, that done, i changed the format d-m-Y in the databaseCelauran 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.