472,145 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

php to get date and time separately into MySQL?

Hello,

at the moment I can add the combined date and time into MySQL using
php

$dt1 = date("Y-m-d H:i:s");

is it possible to add the date and time separately?

I thought it might be

$dt1 = date("Y-m-d");

and

$dt2 = time("H:i:s");

but this doesn't seem to work ...

Cheers

Geoff
Jun 2 '08 #1
6 3838
On Sun, 18 May 2008 01:17:11 +0200, Geoff Cox <gc**@freeuk.notcomwrote:
Hello,

at the moment I can add the combined date and time into MySQL using
php

$dt1 = date("Y-m-d H:i:s");

is it possible to add the date and time separately?

I thought it might be

$dt1 = date("Y-m-d");

and

$dt2 = time("H:i:s");
$dt2 = date("H:i:s");

Why would the funtion name change?
--
Rik Wasmus
....spamrun finished
Jun 2 '08 #2
Geoff Cox wrote:
Hello,

at the moment I can add the combined date and time into MySQL using
php

$dt1 = date("Y-m-d H:i:s");

is it possible to add the date and time separately?

I thought it might be

$dt1 = date("Y-m-d");

and

$dt2 = time("H:i:s");

but this doesn't seem to work ...

Cheers

Geoff
I would agree that while your question IS a PHP question, the solution
may be a MySQL solution. Next time you may consider a cross-post (both
newsgroups in the TO: field :) )

You state "doesn't seem to work"... What is the error and/or result and
what is the result you are expecting. What is the data-type of the
column you are attempting to store this information? CHAR? VARCHAR?
DATE? TIMESTAMP? Just remember, while you can store a date in a
CHAR/VARCHAR column, doing date arithmetic on it will still require
converting it to date/time.

What is the end goal and why? The "why" (short explanation will do)
would determine which one of a thousand methods you could or should be
using to make things easy for you, your app and your db. If your
database has a DATETIME and TIMESTAMP column you could use the MySQL
function NOW() or the PHP time() both of which derive the "unix
timestamp" which is number of seconds from EPOCH time for that platform.
Epoch time for most UNIX OS's is 01/01/1970, for Windows it is
01/01/1980 00:00 (and for OpenVMS (formerly DEC VMS) it is 17-Nov-1858
00:00.

See http://dev.mysql.com/doc/refman/5.0/en/datetime.html for further
explanations of date/time data-types.

Any formating or retrieving time ranges can be derived from this
DATETIME or TIMESTAMP field. I tend to let the database do the database
time and the parse that date/time. **Generally** speaking, time without
a date is pretty useless YMMV. Dates without time *can* be useful for
things like past/future shipping dates etc or any one of many, many more
examples that could be conceived.

IMO saying that something did or will occur at 2PM is pretty useless
without knowing which date. And if this information is required for
reporting that "most queries are performed between 2 and 3AM", it can be
easily derived from the full timestamp which would also require one less
column to maintain.
Jun 2 '08 #3
Michael Austin wrote:
Geoff Cox wrote:
>Hello,

at the moment I can add the combined date and time into MySQL using
php

$dt1 = date("Y-m-d H:i:s");

is it possible to add the date and time separately?

I thought it might be

$dt1 = date("Y-m-d");

and

$dt2 = time("H:i:s");

but this doesn't seem to work ...

Cheers

Geoff

I would agree that while your question IS a PHP question, the solution
may be a MySQL solution. Next time you may consider a cross-post (both
newsgroups in the TO: field :) )
Nope, this is a pure PHP question.
You state "doesn't seem to work"... What is the error and/or result and
what is the result you are expecting. What is the data-type of the
column you are attempting to store this information? CHAR? VARCHAR?
DATE? TIMESTAMP? Just remember, while you can store a date in a
CHAR/VARCHAR column, doing date arithmetic on it will still require
converting it to date/time.

What is the end goal and why? The "why" (short explanation will do)
would determine which one of a thousand methods you could or should be
using to make things easy for you, your app and your db. If your
database has a DATETIME and TIMESTAMP column you could use the MySQL
function NOW() or the PHP time() both of which derive the "unix
timestamp" which is number of seconds from EPOCH time for that platform.
Epoch time for most UNIX OS's is 01/01/1970, for Windows it is
01/01/1980 00:00 (and for OpenVMS (formerly DEC VMS) it is 17-Nov-1858
00:00.

See http://dev.mysql.com/doc/refman/5.0/en/datetime.html for further
explanations of date/time data-types.

Any formating or retrieving time ranges can be derived from this
DATETIME or TIMESTAMP field. I tend to let the database do the database
time and the parse that date/time. **Generally** speaking, time without
a date is pretty useless YMMV. Dates without time *can* be useful for
things like past/future shipping dates etc or any one of many, many more
examples that could be conceived.

IMO saying that something did or will occur at 2PM is pretty useless
without knowing which date. And if this information is required for
reporting that "most queries are performed between 2 and 3AM", it can be
easily derived from the full timestamp which would also require one less
column to maintain.
The problem is he should be using

$dt2 = date("H:i:s");

There are many reasons why you might want date and time data in
different columns. But that is not part of this problem.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jun 2 '08 #4
On Sun, 18 May 2008 01:33:39 +0200, "Rik Wasmus"
<lu************@hotmail.comwrote:
>On Sun, 18 May 2008 01:17:11 +0200, Geoff Cox <gc**@freeuk.notcomwrote:
>Hello,

at the moment I can add the combined date and time into MySQL using
php

$dt1 = date("Y-m-d H:i:s");

is it possible to add the date and time separately?

I thought it might be

$dt1 = date("Y-m-d");

and

$dt2 = time("H:i:s");

$dt2 = date("H:i:s");

Why would the funtion name change?
OK - my mistake - thanks!

Cheers

Geoff
Jun 2 '08 #5
On Sat, 17 May 2008 21:54:05 -0500, Michael Austin
<ma*****@firstdbasource.comwrote:
>Geoff Cox wrote:
>Hello,

at the moment I can add the combined date and time into MySQL using
php

$dt1 = date("Y-m-d H:i:s");

is it possible to add the date and time separately?

I thought it might be

$dt1 = date("Y-m-d");

and

$dt2 = time("H:i:s");

but this doesn't seem to work ...

Cheers

Geoff

I would agree that while your question IS a PHP question, the solution
may be a MySQL solution. Next time you may consider a cross-post (both
newsgroups in the TO: field :) )

You state "doesn't seem to work"... What is the error and/or result and
what is the result you are expecting. What is the data-type of the
column you are attempting to store this information? CHAR? VARCHAR?
DATE? TIMESTAMP? Just remember, while you can store a date in a
CHAR/VARCHAR column, doing date arithmetic on it will still require
converting it to date/time.

What is the end goal and why? The "why" (short explanation will do)
would determine which one of a thousand methods you could or should be
using to make things easy for you, your app and your db. If your
database has a DATETIME and TIMESTAMP column you could use the MySQL
function NOW() or the PHP time() both of which derive the "unix
timestamp" which is number of seconds from EPOCH time for that platform.
Epoch time for most UNIX OS's is 01/01/1970, for Windows it is
01/01/1980 00:00 (and for OpenVMS (formerly DEC VMS) it is 17-Nov-1858
00:00.

See http://dev.mysql.com/doc/refman/5.0/en/datetime.html for further
explanations of date/time data-types.

Any formating or retrieving time ranges can be derived from this
DATETIME or TIMESTAMP field. I tend to let the database do the database
time and the parse that date/time. **Generally** speaking, time without
a date is pretty useless YMMV. Dates without time *can* be useful for
things like past/future shipping dates etc or any one of many, many more
examples that could be conceived.

IMO saying that something did or will occur at 2PM is pretty useless
without knowing which date. And if this information is required for
reporting that "most queries are performed between 2 and 3AM", it can be
easily derived from the full timestamp which would also require one less
column to maintain.

Many thanks for your detailed reply. I will read with case.

I think I have gone back to having date and time together - my problem
was with getting the date/time value into Access but have found a way
round that.

Cheers

Geoff
Jun 2 '08 #6
Rik Wasmus a écrit :
>$dt1 = date("Y-m-d");

and

$dt2 = time("H:i:s");
Btw I'd rather do
$time = time();
$dt1 = date("Y-m-d", $time);
$dt2 = date("H:i:s", $time);

That way, if there is any problem on the server for example, you work
with the same exact time.

Let's say you have a HUGE backup script working at midnight today, using
all of your CPU.

At 23:59:59 today, your page is called, and the first line is (slowly)
parsed.
$dt1 = '2008-05-19';

Then, the second line, due to the CPU being highly used, comes a second
later.
$dt2 = '00:00:00';

And there you go with an incorrect time.

Regards,
--
Guillaume
Jun 2 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Rotsj | last post: by
1 post views Thread by rija | last post: by
3 posts views Thread by dave | last post: by
1 post views Thread by TechnoAtif | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.