How to give or take 3 days in mysql

Have you printed out what’s in $date, and printed out the query, so that you can verify they’re what you expect?

Do some debugging. Make sure every line of code works as expected. Start at the top.

Ok there is some progress now:

When I print $date1 after converson, It prints it in the correct format but incorrect date

it always print 1969-12-31

It should print 2010-05-10 instead. I think the conversion code is incorrect for some reason

this is the conversion code:

$date1 = date(‘Y-m-d’, strtotime($product[“departuredate”]) );

Note If I print $product[“departuredate”] ); on its own before the conversion, it prints the correct date but in the old format e.g 10-05-2010

can you spot any problem with the conversion code?

thanks again

That format must be too ambiguous for strtotime to guess what that date is (as evidenced by you saying it should be May 10th and I would’ve read that as October 5th).

Anyway, for you:

$parts = split("-", $product['departuredate']);
$date1 = $parts[2] . '-' . $parts[1] . '-' . $parts[0];

ok but your code prints the date in this format dd-mm-yyyy e.g. 10-05-2010 I want it to print as yyyy-mm-dd instead because in mysql table the departuredate has this format yyyy-mm-dd

Is that what you meant?

Hmm no, it doesn’t.

//this is what you said $product['departuredate'] holds
$product['departuredate'] = '10-05-2010';

$parts = split("-", $product['departuredate']);
$date1 = $parts[2] . '-' . $parts[1] . '-' . $parts[0];

echo $date1;

Output:

2010-05-10

Which is what you said it should print.

Sorry should have made it clear that it stores a set of dates in the array
I understand how the split works with just one date e.g. ‘10-05-2010’ but with an array of dates like 24/04/2010–26/04/2010–10-05-2010 etc its more difficult. which is what $product[‘departuredate’] holds

The examples shown on tutorials are simpler and basic, could nt find any tutorials that shows how to break the dates etc ?

It works if you set the variable to contain one date only as you did: $product[‘departuredate’] = ‘10-05-2010’; but the variable contains a set of dates so it still prints them as this format
–24/04/2010–17/04/2010–19/04/2010

I am not sure if its the dash at the beginning (“-”, that is confusing it? I tried removing it but it generates a syntax error.

may be we need a way to get rid of the dashes. not sure?

See… you can’t tell me it contains “10-05-2010” if it actually contains dates in “10/05/2010”… of course the code doesn’t work right when you change the precondition…

Look up the split function in the PHP manual and it’ll be clear… you don’t learn if you don’t try to understand the code you’re looking at.

Ok I solved the issue now:

$date1 = $product[‘departuredate’];
list($day, $month, $year) = split( ‘[/.-]’, $date1);
$date3 = "$year-$month-$day<br />
";

echo $date3;

This prints the correct dates and in the correct format

2010-05-12
2010-04-10
etc