473,473 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Storing Dates in DB

What do you think is the best way to store Dates into a database ?
If you want to keep logs or buckups....

I am using date('dmYHis')
but I doesn't work really well ...
Is it better to use date(U) ?

Any suggestions ?

And if you can let me know how you can Display that date back in the Screen
!

Thanks
Jul 17 '05 #1
8 1744
Angelos wrote:
What do you think is the best way to store Dates into a database ?
If you want to keep logs or buckups....

I am using date('dmYHis')
but I doesn't work really well ...
Is it better to use date(U) ?
Any suggestions ?
Use the date or datetime field type - the exact name of the type depends
on your DBMS.
And if you can let me know how you can Display that date back in the
Screen !


Depending which DBMS you are using there may be a function for
formatting the date; in MySQL for example you would use the
date_format() function.

Otherwise you can use the PHP function strtotime() to change it to a
timestamp and then date() to format it as you wish. The downside with
this is you are limited to the range of a unix timestamp which is from
1970 to 2038.

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Jul 17 '05 #2
Ray
Hi

The best way that I found is to use
$date = date("Y-m-d H:i:s");

Works with mysql within a datetime field..
For a date field use date("Y-m-d") and for a time field use date("H:i:s")

Regards
Ray

"Angelos" <an*****@redcatmedia.net> wrote in message
news:d8**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...
What do you think is the best way to store Dates into a database ?
If you want to keep logs or buckups....

I am using date('dmYHis')
but I doesn't work really well ...
Is it better to use date(U) ?

Any suggestions ?

And if you can let me know how you can Display that date back in the
Screen !

Thanks

Jul 17 '05 #3
Chris Hope wrote:
Angelos wrote:
What do you think is the best way to store Dates into a database ?
If you want to keep logs or buckups....

Use the date or datetime field type - the exact name of the type depends
on your DBMS.

Good advice.
Otherwise you can use the PHP function strtotime() to change it to a
timestamp and then date() to format it as you wish. The downside with
this is you are limited to the range of a unix timestamp which is from
1970 to 2038.


strtotime() is very american in its interpretation of dates. Also Unix
timestamps are a bad way to store dates - even if you are running on a 64
bit system (i.e. can work with timestamps after 2036, which isn't so far
away now) you can't work with timestamps before 1970.

HTH

C.
Jul 17 '05 #4
Colin McKinnon <co**************@andthis.mms3.com> wrote:
strtotime() is very american in its interpretation of dates. Also Unix
timestamps are a bad way to store dates - even if you are running on a 64
bit system (i.e. can work with timestamps after 2036, which isn't so far
away now) you can't work with timestamps before 1970.


$ touch -t 191204140000 foo
$ ls -la foo
-rw-r--r-- 1 me me 0 Apr 14 1912 foo
$ stat foo
File: `foo'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: eh/14d Inode: 20099 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 491/ me) Gid: ( 491/ me)
Access: 1912-04-14 00:00:00.000000000 +0019
Modify: 1912-04-14 00:00:00.000000000 +0019
Change: 2005-06-16 16:19:20.000000000 +0200
$ stat -c %X foo
-1821399572
$ php4
<?php
echo date('r',-1821399572);
echo "\n";
?>
Sun, 14 Apr 1912 00:00:00 +0019
$

What do I have here.... a negative unix timestamp... IMHO on most systems
time_t is signed.

The biggest problem is that it is 32bit on most systems.

Jul 17 '05 #5
> Use the date or datetime field type - the exact name of the type depends
on your DBMS.
And if you can let me know how you can Display that date back in the
Screen !


OK so I am using MySQL and I store it in a DATETIME datatype in the MySQL DB
in the folowing format :
$date = date("YmdHis");

So how you would display that ?
Is teh folowing correct ?
date('d M Y',strtotime(row['log_date']))
Jul 17 '05 #6
"Chris Hope" <bl*******@electrictoolbox.com> wrote in message
news:d8**********@lust.ihug.co.nz...
Angelos wrote:
What do you think is the best way to store Dates into a database ?
If you want to keep logs or buckups....

I am using date('dmYHis')
but I doesn't work really well ...
Is it better to use date(U) ?
Any suggestions ?


Use the date or datetime field type - the exact name of the type depends
on your DBMS.
And if you can let me know how you can Display that date back in the
Screen !


Depending which DBMS you are using there may be a function for
formatting the date; in MySQL for example you would use the
date_format() function.

Otherwise you can use the PHP function strtotime() to change it to a
timestamp and then date() to format it as you wish. The downside with
this is you are limited to the range of a unix timestamp which is from
1970 to 2038.


I don't recall date() being limited -

I often use something like $today = date('Y-m-d'); to get a date to enter
into a MySQL database. And when I have a date value that needs translating,
I just use string concatenation: $otherdate = $year . '-' . $month . '-' .
$day; (assuming $year, $month, and $day are numeric, of course)
Jul 17 '05 #7
Tony wrote:
"Chris Hope" <bl*******@electrictoolbox.com> wrote in message
news:d8**********@lust.ihug.co.nz...
Angelos wrote:
What do you think is the best way to store Dates into a database ?
If you want to keep logs or buckups....

I am using date('dmYHis')
but I doesn't work really well ...
Is it better to use date(U) ?
Any suggestions ?


Use the date or datetime field type - the exact name of the type
depends on your DBMS.
And if you can let me know how you can Display that date back in the
Screen !


Depending which DBMS you are using there may be a function for
formatting the date; in MySQL for example you would use the
date_format() function.

Otherwise you can use the PHP function strtotime() to change it to a
timestamp and then date() to format it as you wish. The downside with
this is you are limited to the range of a unix timestamp which is
from 1970 to 2038.


I don't recall date() being limited -

I often use something like $today = date('Y-m-d'); to get a date to
enter into a MySQL database. And when I have a date value that needs
translating, I just use string concatenation: $otherdate = $year . '-'
. $month . '-' . $day; (assuming $year, $month, and $day are numeric,
of course)


date() isn't. strtotime() is as it returns a unix timestamp.

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Jul 17 '05 #8
Colin McKinnon wrote:
Chris Hope wrote:
Angelos wrote:
What do you think is the best way to store Dates into a database ?
If you want to keep logs or buckups....

Use the date or datetime field type - the exact name of the type
depends on your DBMS.

Good advice.
Otherwise you can use the PHP function strtotime() to change it to a
timestamp and then date() to format it as you wish. The downside with
this is you are limited to the range of a unix timestamp which is
from 1970 to 2038.


strtotime() is very american in its interpretation of dates. Also Unix
timestamps are a bad way to store dates - even if you are running on a
64 bit system (i.e. can work with timestamps after 2036, which isn't
so far away now) you can't work with timestamps before 1970.


Hence my note about the downside :)

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Robert | last post by:
I have no problem storing dates + times in a System.DateTime object. In addition, it's easy to output a Time as a string from an existing Date/Time. But I'm having trouble storing a time only. ...
1
by: Thomas R. Hummel | last post by:
Hello all, I have been working with several databases here that are basically data marts. A lot of the entities have an attribute that is a particular year and month. For example, a financial...
8
by: Zvonko | last post by:
Hi! I would like to get the clients date and time (his system time) and store it somewhere so I can use it in my code later. (insert it to database!). Any ideas Zvonko
5
by: dd_bdlm | last post by:
Hi all I have a database that stores customer records and their associated insurance details. I need to be able to track any changes made to that record within a set time period ie if details...
10
by: IntraRELY | last post by:
Here is my code, but get errors: Dim installment = 1 Dim beginDate = "1/1/03" Dim endDate = "1/1/08" Dim dates(5) As Array While installment <= 5 endDate = DateAdd(DateInterval.Year, 1,...
7
by: fauxanadu | last post by:
Is it possible to store dates before 01/01/0100 A.D. (such as for as database storing world events would require) using MS Access? Verbose Explination I need to be able to store dates before...
2
by: billelev | last post by:
Hi there, Does anyone know how to store a constant in VBA that is set using a function or sub-routine? Here is the scenario. When I load up Access for the first time, I'd like to calculate...
1
by: nightscorpion | last post by:
Hello everyone. i have 2 dates given FromDate :1998-04-13T00:00:00 ToDate :1998-04-18T23:59:00 i would like to store these two days as single dates in the table in my db could...
4
by: John A Grandy | last post by:
What are some best practices for storing pure dates and pure times in .NET ? I notice that DateTime.TimeOfDay() returns type TimeSpan , which is certainly sufficient for storing pure times , but...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.