You are here: Re: PHP math with MySQL date format « PHP Programming Language « IT news, forums, messages
Re: PHP math with MySQL date format

Posted by pritaeas on 12/23/07 16:18

"Puzzled" <scratching.head@example.com> wrote in message
news:6drsm3l1m7sn9gd24p76g9p2r5lnblbshl@4ax.com...
> On Sun, 14 Oct 2007 07:07:24 -0400,
> bill <nobody@spamcop.net> wrote:
>
>>> SELECT
>>> something,
>>> datecolumn,
>>> DATEDIFF(datecolumn,CURDATE())
>>> FROM table
>>>
>>>
>>>> Do I need to convert it to a timestamp in order to do the math, or is
>>>> there an easier way ?
>>>
>>> Otherwise it's just a string, because PHP has no 'date' type. So either
>>> split it into components or indeed convert it to a timestamp (which
>>> would be easiest).
>>>
>>
>>now back to PHP...
>>If I do the select as you noted above, after I have gotten the
>>row, what is the index..
>>e.g: $row['?????']
>
> Let's say the name of your date field is 'LastUpdate', and your
> table is 'purchases' (for this example, you're trying to find out
> how long ago someone made a purchase). So to do it, you go
>
> $dataset = mysql_query( 'SELECT Name, DATEDIFF( LastUpdate,
> CURDATE()) FROM purchases' ) ;
>
> This presumably yields as many values as you have customers.
> Since you specified Name, then DATEDIFF, those values are in the
> [0] and [1] elements respectively, so:
>
> while ( $row = mysql_fetch_row($dataset) )
> {
> echo $row[1].' last bought something '.
> $row[0].' days ago<br>' ; // print out all the deltas
> }
>
> HTH

$dataset = mysql_query( 'SELECT Name, DATEDIFF( LastUpdate, CURDATE()) AS
column_datediff FROM purchases' ) ;

Now you can access
$row["column_datediff"];

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация