By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,262 Members | 2,664 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,262 IT Pros & Developers. It's quick & easy.

php to get date and time separately into MySQL?

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.