Connecting Tech Pros Worldwide Help | Site Map

php how to treat mysql field like a date?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 01:36 AM
Yulia Yegenov
Guest
 
Posts: n/a
Default php how to treat mysql field like a date?

I have a query that looks like this: (I insert the date created with the php
date function)

$status = "Active";

//(I cannot use the mysql timestamp function for other reason).
$curdate = date("m/j/Y H:i");
$owner = "not assigned";

@ $db_connect = mysql_connect("localhost", "$db_username", "$db_password");
if (!$db_connect)
{
echo "Unable to connect to the database;
exit;
}
mysql_select_db("$db");
$query = "insert into tickets (user, status, loannum, problem, description,
multiples, ticketnumber, ip, office, submissiontime, ticketowner) values
('$user1', '$status', '$loannum', '$problem', '$description', '$multiples',
'$ticketnumber', '$ip', '$office', '$curdate', '$owner')";
$result = mysql_query($query);

I need to pull out the last fifty records from the database, so I use this
query:

$order = $order;
if ($order ==""){
$order = "user";
}

mysql_select_db("$db");
$query = "select * from tickets ORDER by $order";
$result = mysql_query($query);
$num = mysql_num_rows($result);


if I order the tickets by submission time, it orders the items, but not
correctly. It looks like php treating the submissiontime like integer:
example:

12/1/2003 16:51

12/1/2003 16:58

12/10/2003 12:29

12/10/2003 12:36

12/11/2003 07:10

12/2/2003 09:16



How can I tell php treat the submissiontime field in mysql as a date so it
can be ordered correctly? I need to do this so I can do some maths and some
sorting on the the date.



TIA

Yulia



  #2  
Old July 17th, 2005, 01:36 AM
Senator Jay Billington Bulworth
Guest
 
Posts: n/a
Default Re: php how to treat mysql field like a date?

In article <fPadnQQ0M5JfNkWiU-KYjA@giganews.com>,
"Yulia Yegenov" <yuliay@nospam.noadd.ru> wrote:
[color=blue]
> I have a query that looks like this: (I insert the date created with the php
> date function)
>
> //(I cannot use the mysql timestamp function for other reason).
> $curdate = date("m/j/Y H:i");
>
> [major snip]
>
> if I order the tickets by submission time, it orders the items, but not
> correctly. It looks like php treating the submissiontime like integer:[/color]

Actually it's MySQL doing that, but you're right about the order.
[color=blue]
> 12/1/2003 16:51
>
> 12/1/2003 16:58
>
> 12/10/2003 12:29
>
> How can I tell php treat the submissiontime field in mysql as a date so it
> can be ordered correctly? I need to do this so I can do some maths and some
> sorting on the the date.[/color]

Use date('m/d/Y H:i') instead, so that the day gets zero-padded. This
should get MySQL to sort them the way you need, e.g. 12/01 will come
before 12/10.

Or, you could use a datetime column in the MySQL table (assuming you
aren't already).

hth

--
Bulworth : funha@fung.arg | My email address is ROT13 encoded, decode to mail
--------------------------|--------------------------------------------------
<http://www.phplabs.com/> | PHP scripts and thousands of webmaster resources!
  #3  
Old July 17th, 2005, 01:38 AM
Don Crossman
Guest
 
Posts: n/a
Default Re: php how to treat mysql field like a date?

"Yulia Yegenov" <yuliay@nospam.noadd.ru> wrote in
news:fPadnQQ0M5JfNkWiU-KYjA@giganews.com:
[color=blue]
> I have a query that looks like this: (I insert the date created with
> the php date function)
>
> $status = "Active";
>
> //(I cannot use the mysql timestamp function for other reason).
> $curdate = date("m/j/Y H:i");
> $owner = "not assigned";
>
> @ $db_connect = mysql_connect("localhost", "$db_username",
> "$db_password"); if (!$db_connect)
> {
> echo "Unable to connect to the database;
> exit;
> }
> mysql_select_db("$db");
> $query = "insert into tickets (user, status, loannum, problem,
> description, multiples, ticketnumber, ip, office, submissiontime,
> ticketowner) values ('$user1', '$status', '$loannum', '$problem',
> '$description', '$multiples', '$ticketnumber', '$ip', '$office',
> '$curdate', '$owner')"; $result = mysql_query($query);
>
> I need to pull out the last fifty records from the database, so I use
> this query:
>
> $order = $order;
> if ($order ==""){
> $order = "user";
> }
>
> mysql_select_db("$db");
> $query = "select * from tickets ORDER by $order";
> $result = mysql_query($query);
> $num = mysql_num_rows($result);
>
>
> if I order the tickets by submission time, it orders the items, but
> not correctly. It looks like php treating the submissiontime like
> integer: example:
>
> 12/1/2003 16:51
>
> 12/1/2003 16:58
>
> 12/10/2003 12:29
>
> 12/10/2003 12:36
>
> 12/11/2003 07:10
>
> 12/2/2003 09:16
>
>
>
> How can I tell php treat the submissiontime field in mysql as a date
> so it can be ordered correctly? I need to do this so I can do some
> maths and some sorting on the the date.
>
>
>
> TIA
>
> Yulia
>
>[/color]

Or, you might do something like "select * from tickets order by
unix_timestamp($order) desc limit 50". This would get the newest 50
records.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.