Web Hosting Talk







View Full Version : [PHP] Specified date + 1 day?


jonathanbull
01-12-2007, 04:22 PM
Hi,

After writing a script for about 2 days it seems I've hit a stumbling block at one of the simplest points... I can't for the life of me figure out how to find out the day after a specified date.

For example:

$date = "2005-08-31";

I want to be able to calculate:

$dateplusone = "2005-09-01";


I've searched Google and it seems that I need to use mktime. However, all of the example scripts seem to refer to calculating tomorrows date.


Can anybody help!? It'd be greatly appreciated! :)


Thank you in advance,

Dan L
01-12-2007, 05:07 PM
$date = explode('-',$date);
$year = $date[0];
$month = $date[1];
$day = $date[2];
$newDate = mktime(0,0,0,$month,$day,$year);
echo strftime('%Y-%m-%d',$newDate);

That's all you need to do. You can simply add or subtract from either the year, month, or day, and it will calculate the new one. Even if the month is 12, it will compensate for it.

zoid
01-12-2007, 05:08 PM
Yes mktime() is right. Use it to determine the day's timestamp, then add 86400 (24 hours in seconds) and convert the result with date().
<?php echo date('M d Y', mktime(0, 0, 0, 8, 31, 2005)+86400); ?>

jonathanbull
01-12-2007, 05:25 PM
Thank you both very much for your replies - they worked a treat!

I'm using the script to determine which day my website received the most signups.

I loop through each date and count the occurences in the 'day of signup' column in my mySQL table.

It seems however that my table is too large and the script takes forever to load - I've got a feeling there must be a much quicker way of doing this, am I right?


Thank you once again for your help.

zoid
01-12-2007, 05:43 PM
That's all you need to do. You can simply add or subtract from either the year, month, or day, and it will calculate the new one. Even if the month is 12, it will compensate for it.
I didnt suppose this would work (although it actually seems typical for PHP on a second thought). Its definitely the dirtier solution but probably the more natural over mine. I guess I would recommend it over mine, at least for PHP.

I'm using the script to determine which day my website received the most signups.

I loop through each date and count the occurences in the 'day of signup' column in my mySQL table.

It seems however that my table is too large and the script takes forever to load - I've got a feeling there must be a much quicker way of doing this, am I right?
I'd say you could achieve this also with a SQL query, but one would need to know your database scheme for more information.

I havent tested it but I could imagine something like this could work
SELECT signup_date, COUNT(*) FROM my_table GROUP BY signup_date

bigfan
01-12-2007, 08:32 PM
I think zoid's solution (doing it with a query) is the best, but for adding a day with PHP, hasn't anyone ever come across strtotime()?$date_plus_one = date('Y-m-d', strtotime($date . '+1 day'));

host2020
01-12-2007, 10:36 PM
strtotime will do the trick. Tomorrow would be:
strtotime('+1 day');

This site is a good reference (I can't post real URLs yet)
www[dot]the-art-of-web[dot]com/php/strtotime/

HIU-Daniel
01-15-2007, 03:32 PM
Thank you both very much for your replies - they worked a treat!

I'm using the script to determine which day my website received the most signups.

I loop through each date and count the occurences in the 'day of signup' column in my mySQL table.

It seems however that my table is too large and the script takes forever to load - I've got a feeling there must be a much quicker way of doing this, am I right?


Thank you once again for your help. It would seem that you need to create an index. Try looking at this tutorial http://www.databasejournal.com/features/mysql/article.php/1382791