Jump to content

Archived

This topic is now archived and is closed to further replies.

Seb Spiers

Manipulating Strings as Dates

Recommended Posts

Hi There,

Ive got a simple html form with a text field in which a date is entered in a dd/mm/yyyy format.
I want to be able to manipulte the date by adding and subtracting days.

The problem i've found is that if I enter 22/06/1983 and for example do; [code]<? echo ( $date_commencing + 6 ) ; ?>[/code]The output is not 28/06/1983, it simply outputs "28".  It uses the numbers before the first "/".  I want to handle the date as a date.

Can anyone help? :)

Ta! :X

Share this post


Link to post
Share on other sites
Use the strtotime (http://www.php.net/strtotime), maketime (http://www.php.net/maketime), and date http://www.php.net/date) functions to accomplish what you want.

Essentially, use strtotime to change your string to a unix timestamp, then use maketime or date to add 6 days.

Share this post


Link to post
Share on other sites
[code]<?php
function add_days($current_time,$days,$format='d/m/Y')
{
return date($format,strtotime($current_time)+86400*$days);
}

echo add_days('22/06/1983','6');
?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.