How to increment Dates retrieved with queries

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply

How much (in US Dollars) damage would you estimate you did to hardware and facilities while learning PhP?

Poll ended at Sun Aug 03, 2003 10:47 am

$0 - Cuz I'm a freekin genius!
2
33%
$50-150 - Mice and Trackballs just don't bounce very well.
4
67%
$151-250 - Keyboards don't respond much better
0
No votes
$251-500 - We won't ask what happened to the sheetrock in your room
0
No votes
> $500 - Let us know when the insurance settlement is in.
0
No votes
 
Total votes: 6

User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

How to increment Dates retrieved with queries

Post by steedvlx »

Hello once again.

I am wondering how I might go about adding a certain number of days to the date retrieved in the following query. Obviously the code "$auction_end = ($data["curdate()"] + 7);"
doesn't work. :D

Code: Select all

<?php
mysql_select_db($database_admin_conn, $admin_conn);
$query_rs_date_time = "SELECT curdate(), curtime();";
$result = mysql_query($query_rs_date_time, $admin_conn) or die(mysql_error("screwed again"));
$data = mysql_fetch_array($result);
$date = $data["curdate()"];
$auction_end = ($data["curdate()"] + 7);
?>
I am shocked to realize from reading that PhP relies on the database to handle most of its date and time functions. I'd really like to just say <? "YO! add 7 days to $date and put it in $date_plus_7"; ?> (LOL)

Any help appreciated..

----------------------------
SteedVLX
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Post by steedvlx »

BTW, the poll is attached because I fragmented a trackball earlier today because I couldn't figure out why a database connection kept returning a "method not allowed" error.

Figured out the problem (Dreamweaver bug!). But, now using an old logitech PS2 trackball.

*sigh*
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Have you looked at PHP's date(), strtodate(), etc functions in the manual? PHP does NOT depend on a database for date processing.

Code: Select all

$timestamp = time(); // gives you the current timestamp
$day_of_month = date("d",$timestamp); // gets the day of month from timestamp
$day_of_year = date("z",$timestamp); // gets day of year (1-366)
$seven_days_later = mktime(0,0,0,date("m"), date("d")+7,date("Y"));
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Post by steedvlx »

You're right of course. I should have specified that I don't know how to work in linux timestamps or GMT/CUT time formats.

I would like to find something that tells me in plain English how to manipulate dates in good old YYYY-MM-DD format and just add a certain number of days (or even months and years) to it and have it come out in the same friendly format.

I noticed the code you suggest for $seven_days_later gives me "1060354800". Unfortunately, I don't know what to do with that. would you mind continuing the discussion a little further to formatting that date like MySQL gives you when you retrieve "curdate()"? I still can't find anything in my beginners books, and the official manual just makes me dive for a translator.

My book gives me only this;
"date(format [,timestamp]) RETURNS STRING Formats a local time/date"
This is all it has to say on this function. They don't even give an example so I can figure out what they mean by the "format" entry.
Thanks

----------------------------
SteedVLX
User avatar
MrNonchalant
Forum Commoner
Posts: 29
Joined: Wed Jul 17, 2002 2:15 am

Post by MrNonchalant »

To have strings like "1060354800" come out in YYYY-MM-DD use:

Code: Select all

$theDateIsThus = date("Y-m-d",$timestampVariable);
That in conjunction with the code above will give you what you want.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

OK given a date in YYYY-MM-DD

Code: Select all

$date = "2003-08-01"; // today
$timestamp = strtotime($date);
$timestamp_7 = mkdate(0,0,0,date("m",$timestamp),
                                   date("d",$timestamp)+7, 
                                   date("Y",$timestamp));
$date_7 = date("Y-m-d",$timestamp_7);
The format string tells the date command how to format the timestamp. The manual has a huge number of format examples..... Its definately worth learning to work with timestamps as almost every languange uses them.
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Post by steedvlx »

thanks to both of you. The answers were essentially identical, so I just used your examples and it worked out great. If any other newbies are as confused over date/time formatting as I am, here is code that will give you;

today's date ($date),
the current time ($time), and
the date one week from today ($auction_end)

All in 'standard' format.

Code: Select all

<?php
mysql_select_db($database_admin_conn, $admin_conn);
$query_rs_date_time = "SELECT curdate(), curtime();";
$result = mysql_query($query_rs_date_time, $admin_conn) or die(mysql_error("screwed yet again"));
$data = mysql_fetch_array($result);
$date = $data["curdate()"];
$time = $data["curtime()"];
$date_plus_seven = mktime(0,0,0,date("m"), date("d")+7,date("Y")); 
$auction_end = date("Y-m-d",$date_plus_seven);
?>
Enjoy! and Thanks again.
---------------------------
SteedVLX
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

You don't need to use MySQL at all, though

Code: Select all

$date = date("Y-m-d");
$time = date("h:m:s");
Note, your $date = $data["curdate()"]; is never used in your sample code. The call in mktime to date("m") returns the current month, as no extra timestamp was passed in.
So you could reduce the entire thing to

Code: Select all

$auction_end = date("Y-m-d",mktime(0,0,0,date("m"),date("d")+7,date("Y")));
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Post by steedvlx »

Jeez! that is SO much simpler than the way this book explains handling standard formats of date and time!

It would have helped if they had at least given the examples you folks have just showed me. At least now, I can see how it works without all the GMT and timestamp format mumbo-jumbo. (I will learn that mumbo-jumbo though, just later).

For the record, this book I have taught the MySQL approach and totally ignored the simpler (and pure PHP) handlings I have just learned here. The title is "BEGINNING PHP4" I don't understand that at all.

As always, I appreciate all of you taking the time to help me.

cheers
------------------------------
SteedVLX
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Is thisthe Wrox Beg PHP4 book? or another company> I've normally been impressed with Wrox's books, but I do avoid their "Beginner" books, preferring the "Pro"/"Advanced" series.
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Post by steedvlx »

Well you know I really don't like to name names or anything like that. but.

YES IT WAS THEM. IT'S THEIR FAULT! :oops:

Anyway, I really feel that professionals should not be permitted to write beginners programming books without a committee of at least 5 beginners on the editorial staff. 8O :)

I know from experience how hard it is to teach beginners what I know how to do in my sleep. It is very hard to predict where they will get tripped-up. I'm sure the guys at Wrox write great intermediate and advanced books. And, I can't wait to be able to understand them.

----------------------------------
SteedVLX
jmarcv
Forum Contributor
Posts: 131
Joined: Tue Jul 29, 2003 7:17 pm
Location: Colorado

Post by jmarcv »

Why use a book when there is php.net?
try this:

Code: Select all

$nextweek=date('Y-m-d',strtotime ("+7 day"));
Post Reply