Norwald2
Norwald2

Reputation: 121

How to handle date and time with PHP?

I need to do a little bit more complicated calculations with time and date values from my MySQL database with PHP.

I need to add or substract different values from a given date.

For example:

Please note that there is a difference between substracting 1 Month, 4 Weeks or 30 Days.

What's the preferred way of doing this? Is there any clever library or can I do this with PHP's own functions?

Upvotes: 5

Views: 4545

Answers (10)

xPheRe
xPheRe

Reputation: 2343

Since PHP 5.2 you have the DateTime object. You can do:

$date = new DateTime; // now
$date->modify('1 month ago');
echo $date->format('Y-m-d');

Check this link for the complete sintax.

Upvotes: 4

zerodin
zerodin

Reputation: 877

here's another suggestion. if you want to manipulate information from mysql, try using the date/time functions provided by mysql here

Upvotes: 0

Bryan M.
Bryan M.

Reputation: 17322

You can use a combination of PHP5's DateTime and DateInterval objects.

$now = new DateTime();

// 30 days ago
$now->sub(new DateInterval("P30D");

// 1 week ago
$now->sub(new DateInterval("P1W");

// 2 years from now
$now->add(new DateInterval("P1Y");

For a list of interval codes, see the manual

Upvotes: 5

Galen
Galen

Reputation: 30170

Depending on your application you're going to want to either use the mysql datetime functions

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add

or strtotime like everyone else has said

Upvotes: 0

Evan Mulawski
Evan Mulawski

Reputation: 55334

<?php
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('10 days'));
echo date_format($date, 'Y-m-d');
?>

$date will now be '2000-01-11'.

You can also use date_sub to subtract an amount of time.

More here:

http://www.php.net/manual/en/datetime.add.php http://www.php.net/manual/en/datetime.sub.php

Upvotes: 1

Sergey K
Sergey K

Reputation: 4114

look at this please PHP dates

Upvotes: 0

Ass3mbler
Ass3mbler

Reputation: 3915

try Pear Date library. Very powerful and flexible, with period adding capabilities. It does exactly what you want.

http://pear.php.net/Date

We use it in a very date-intensive web application, where we have to add a lot of time variable periods and calculate week number etc.

Upvotes: 0

Hamish
Hamish

Reputation: 23316

The PHP function strtotime is very powerful in this regard.

For example:

// 1 month ago
$date = strtotime('-1 month');

// 30 days ago
$date = strototime('-30 days');

Upvotes: 1

drew
drew

Reputation: 1312

From http://php.net/manual/en/function.strtotime.php

<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>

You can add a second parameter to make it add to given time:

echo strtotime("+1 week",$timestamp)

Upvotes: 7

Nick Shepherd
Nick Shepherd

Reputation: 544

Check out the strtotime() function. http://php.net/manual/en/function.strtotime.php

Also note that you can use the second parameter to dictate when to start from.

strtotime('+1 day', mktime(0, 0, 0, 7, 1, 2000));

Upvotes: 0

Related Questions