Add to Favorites

useful php function, strtotime

PHP has a lovely little function called strtotime. Many of you know how useful it is, but some of you may be looking for an easy quick way to do some simple date calculations.If you wanted to find what day is two weeks and 1 day from now, you could do something as simple as:

[code]

$the_day = date('m/d/Y',strtotime('+2 week +1 day'));

[/code]

It is really that simple. You can also use this function for a quick conversion of user input/database fields to a Unix timestamp for further manipulation.

[code]

$database_time = strtotime( $database_arr['date'] );
// strtotime will handle the common format YYYY-MM-DD

$user_input_date = strtotime( $_POST['date'] );

[/code]

strtotime will handle a variety of date formats, almost anything a user could throw at you such as:

  • January 3, 2008
  • Jan 3, 2008
  • 01/03/2008
  • next thursday

If the user does happen to give you something that strtotime can't recognize, it will return false ( as long as you have a later version of php, otherwise it may return -1 )

So there you have it, the useful little function strtotime.

Comments

Be the first to leave a comment on this post.

Leave a comment

To leave a comment, please log in / sign up