473,382 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 3915
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: perplexed | last post by:
How do you convert a user inputted date to a unix timestamp before insterting it into your database? I have a form, with a textfield for a date that the user inputs in the format mm-dd-yyyy and...
1
by: Dave Posh | last post by:
I seem to be having a problem displaying time stored in mysql. The format stored in the database is 13:15:05. The database data type is time. I'm using asp vbscript and sql to retrieve the time...
1
by: Rotsj | last post by:
Hi, i'm using visual foxpro 9 with a mysql 5 database, for direct access to my database i use navicat. In visual foxpro i've set my date format to dd-mm-yyyy, also i did this in navicat. However...
1
by: rija | last post by:
Hi folks, I need help regarding date and time comparison in PHP and MySQL My website is hosted in the USA (GMT - 8) And It is managed in Madagascar (GMT + 3) Want to consider DATE in Madagascar...
6
by: marc | last post by:
hi im trying to convert Date() into a unix timestamp so i can stick the result into a mysql db, please help!
3
by: dave | last post by:
I need to compute an expiration date based on the number of hours, days, or months purchased. The expiration date needs to be expressed in minutes something like '1260481600'. How can I get the...
1
by: TechnoAtif | last post by:
Hi All. I have to incorporate a DOJO datetimepicker control in PHP.I have used date picker and time picker separately.However am not able to find a way to use date and time together as...
10
by: WebCM | last post by:
There is a function: http://paste.ubuntu.com/21865 It needs GMT date in YYYY-MM-DD HH:MM:SS format - in SQL: datetime. If date is the same as today, the function returns "Today". There is one...
4
by: ahmurad | last post by:
Dear Brothers, I am struggling the following four Date-Time type values which were inputted into MYSQL database in different tables. As MYSQL Default Time Format: YYYY-MM-DD HH:MM:SS, So I used...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.