Jump to content


Photo

Manipulating Strings as Dates


  • Please log in to reply
2 replies to this topic

#1 Seb Spiers

Seb Spiers
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 20 August 2006 - 02:29 PM

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;
<? echo ( $date_commencing + 6 ) ; ?>
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

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 20 August 2006 - 02:41 PM

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.

#3 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 20 August 2006 - 02:53 PM

<?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');
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users