RE: [PHP] syntax for date math expressions
Convert to date/time variable and perform arithmetic. Otherwise, if these dates are from mysql, let mysql do it -Original Message- From: ROBERT MCPEAK [mailto:[EMAIL PROTECTED]] Sent: Wednesday, April 03, 2002 11:12 AM To: [EMAIL PROTECTED] Subject: [PHP] syntax for date math expressions What is the php syntax for adding or subtracting dates? For example, I'd like to do something like this: 2002-04-03 - 2002-04-02 = 1 or 2002-04-03 - 2002-04-02 = -00-01 or 2002-04-03 + -00-01 = 2002-04-03 etc. Can anybody help this newbie? Thanks! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] syntax for date math expressions
this is an option that only sort of works (converting to date/time); $x = strtotime(2002-04-03) - strtotime(2002-04-02); $x will contain 86400 which is the number of seconds in a normal day. Since the days that some people use switch from daylight savings time to standard time (and visa versa) there are a different number of seconds in those days so simply dividing by some fixed value will not work (this is what I mean by sort of). Some databases have a datadiff function, perhaps you can pass a query something like SELECT datediff(2002-04-03,2002-04-02) as difference from realtable Warren Vail Tools, Metrics Quality Processes (415) 667-7814 Pager (877) 774-9891 215 Fremont 02-658 -Original Message- From: Rick Emery [mailto:[EMAIL PROTECTED]] Sent: Wednesday, April 03, 2002 11:02 AM To: 'ROBERT MCPEAK'; [EMAIL PROTECTED] Subject: RE: [PHP] syntax for date math expressions Convert to date/time variable and perform arithmetic. Otherwise, if these dates are from mysql, let mysql do it -Original Message- From: ROBERT MCPEAK [mailto:[EMAIL PROTECTED]] Sent: Wednesday, April 03, 2002 11:12 AM To: [EMAIL PROTECTED] Subject: [PHP] syntax for date math expressions What is the php syntax for adding or subtracting dates? For example, I'd like to do something like this: 2002-04-03 - 2002-04-02 = 1 or 2002-04-03 - 2002-04-02 = -00-01 or 2002-04-03 + -00-01 = 2002-04-03 etc. Can anybody help this newbie? Thanks! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] syntax for date math expressions
On Wed, 3 Apr 2002, Rick Emery wrote: Convert to date/time variable and perform arithmetic. Otherwise, if these dates are from mysql, let mysql do it Even if you don't happen to be getting the date out of MySQL, it can occasionally be easier to let MySQL do your date math since it has some nice functions for it (DATE_ADD, DATE_SUB, etc.). These things are hard to do quickly in PHP since you have to account for leap years. You can send MySQL (or really any SQL database) a select with hardcoded inputs rather than pulling from tables. miguel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] syntax for date math expressions
On Wednesday, April 3, 2002, at 03:58 PM, Miguel Cruz wrote: On Wed, 3 Apr 2002, Rick Emery wrote: Convert to date/time variable and perform arithmetic. Otherwise, if these dates are from mysql, let mysql do it Even if you don't happen to be getting the date out of MySQL, it can occasionally be easier to let MySQL do your date math since it has some nice functions for it (DATE_ADD, DATE_SUB, etc.). These things are hard to do quickly in PHP since you have to account for leap years. I'd actually like to use MySQL's builtin date formats, but I like the timestamp that PHP uses (and I like what I can do with a timestamp in the date() function) so I have been using VARCHAR(20) to hold the date as a string. MySQL's TIMESTAMP is not the same thing as PHP's. Erik Erik Price Web Developer Temp Media Lab, H.H. Brown [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] syntax for date math expressions
I'd actually like to use MySQL's builtin date formats, but I like the timestamp that PHP uses (and I like what I can do with a timestamp in the date() function) so I have been using VARCHAR(20) to hold the date as a string. MySQL's TIMESTAMP is not the same thing as PHP's. But you can simply call MySQL's UNIX_TIMESTAMP() function on the mysql field when you select it if you want it into unix timestamp format. -Rasmus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] syntax for date math expressions
On Wed, 3 Apr 2002, Erik Price wrote: On Wednesday, April 3, 2002, at 03:58 PM, Miguel Cruz wrote: Even if you don't happen to be getting the date out of MySQL, it can occasionally be easier to let MySQL do your date math since it has some nice functions for it (DATE_ADD, DATE_SUB, etc.). These things are hard to do quickly in PHP since you have to account for leap years. I'd actually like to use MySQL's builtin date formats, but I like the timestamp that PHP uses (and I like what I can do with a timestamp in the date() function) so I have been using VARCHAR(20) to hold the date as a string. MySQL's TIMESTAMP is not the same thing as PHP's. You can also convert back and forth between UNIX_TIMESTAMP and MySQL's native timestamp format as you please. miguel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] syntax for date math expressions
On Wednesday, April 3, 2002, at 04:14 PM, Rasmus Lerdorf wrote: But you can simply call MySQL's UNIX_TIMESTAMP() function on the mysql field when you select it if you want it into unix timestamp format. For SELECTs, this is fine, but what happens when I want to insert a new date? I transform all of my dates into Unix timestamps so that I can pass them around in form fields etc, but MySQL's timestamp format is different. Is there some way to call something like UNIX_TO_MYSQL_TIMESTAMP() in MySQL (or PHP)? Erik Erik Price Web Developer Temp Media Lab, H.H. Brown [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] syntax for date math expressions
date() On Wed, 3 Apr 2002, Erik Price wrote: On Wednesday, April 3, 2002, at 04:14 PM, Rasmus Lerdorf wrote: But you can simply call MySQL's UNIX_TIMESTAMP() function on the mysql field when you select it if you want it into unix timestamp format. For SELECTs, this is fine, but what happens when I want to insert a new date? I transform all of my dates into Unix timestamps so that I can pass them around in form fields etc, but MySQL's timestamp format is different. Is there some way to call something like UNIX_TO_MYSQL_TIMESTAMP() in MySQL (or PHP)? Erik Erik Price Web Developer Temp Media Lab, H.H. Brown [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] syntax for date math expressions
as a string. MySQL's TIMESTAMP is not the same thing as PHP's. Let me be the devil's layer here, Erik. In order to achieve the best portability and to gain the best performance you'd better use mySQL date field types to store the dates. If you need the PHP timestamps you can simply use another date function of mySQL (which was that?) on the SELECT and receive your data already in UNIX timestamp values. There's no way you can prove to me that PHP's timestamp is better in the mySQL's varchar(20) (20?? Why not 10 then and why not INT? Enough or the next 320 years or so... we just recently turned to 10m) instead of the simple timestamp(14). Sincerely, Maxim Maletsky Founder, Chief Developer PHPBeginner.com (Where PHP Begins) [EMAIL PROTECTED] www.phpbeginner.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] syntax for date math expressions
Just insert the date: like NOW(), or whatever formatted string into the field PS: YAY! In Attack! Sincerely, Maxim Maletsky Founder, Chief Developer PHPBeginner.com (Where PHP Begins) [EMAIL PROTECTED] www.phpbeginner.com -Original Message- From: Erik Price [mailto:[EMAIL PROTECTED]] Sent: Wednesday, April 03, 2002 11:27 PM To: Rasmus Lerdorf Cc: [EMAIL PROTECTED] Subject: Re: [PHP] syntax for date math expressions On Wednesday, April 3, 2002, at 04:14 PM, Rasmus Lerdorf wrote: But you can simply call MySQL's UNIX_TIMESTAMP() function on the mysql field when you select it if you want it into unix timestamp format. For SELECTs, this is fine, but what happens when I want to insert a new date? I transform all of my dates into Unix timestamps so that I can pass them around in form fields etc, but MySQL's timestamp format is different. Is there some way to call something like UNIX_TO_MYSQL_TIMESTAMP() in MySQL (or PHP)? Erik Erik Price Web Developer Temp Media Lab, H.H. Brown [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] syntax for date math expressions
In two words - here's your bible, Erik: http://www.mysql.org/documentation/mysql/bychapter/manual_Reference.html #Date_and_time_functions Sincerely, Maxim Maletsky Founder, Chief Developer PHPBeginner.com (Where PHP Begins) www.phpbeginner.com [EMAIL PROTECTED] -Original Message- From: Erik Price [mailto:[EMAIL PROTECTED]] Sent: Wednesday, April 03, 2002 11:27 PM To: Rasmus Lerdorf Cc: [EMAIL PROTECTED] Subject: Re: [PHP] syntax for date math expressions On Wednesday, April 3, 2002, at 04:14 PM, Rasmus Lerdorf wrote: But you can simply call MySQL's UNIX_TIMESTAMP() function on the mysql field when you select it if you want it into unix timestamp format. For SELECTs, this is fine, but what happens when I want to insert a new date? I transform all of my dates into Unix timestamps so that I can pass them around in form fields etc, but MySQL's timestamp format is different. Is there some way to call something like UNIX_TO_MYSQL_TIMESTAMP() in MySQL (or PHP)? Erik Erik Price Web Developer Temp Media Lab, H.H. Brown [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php