Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 02-19-2009, 06:23 PM Saving date to dbase
Skilled Talker

Posts: 68
Name: Avi Zolty
Location: Atlanta
Trades: 0
I have tried playing around with different data-types, but nothings working.

I just want to save a date and time the 'TheDate' field in my database... heres what i've tried.

WITH A TIMESTAMP DATA TYPE
$datevar = time();
$sql="INSERT INTO Persons (TheDate)
VALUES
($datevar)";
but when I call it in it outputs all 0's

WITH A TIMESTAMP DATA TYPE
$datevar = time();
$sql="INSERT INTO Persons (TheDate)
VALUES
($datevar)";
and then when I'm calling it in i do the following (the $row because it's coming out in a table):
date("d-m-y h:i:s", $row['TheDate']

WITH A DATETIME DATA TYPE
$datevar = date("Y-m-d",time());
$sql="INSERT INTO Persons (TheDate)
VALUES
($datevar)";
but when I call it in it reads the -'s as minus signs and subtracts the month and day... (similarly with dividing the '/' signs) I've tried ALL sorts of combinations with the diff letters (ymdhis..etc) and everything... but sometimes it calculates and sometimes it says my query was invalid and that i should check my syntax (it says check the syntax before "" and tells me the last few digits of my time)

I've also tried to save with a varchar datatype
$datevar = ("d-m-y",time());
$sql="INSERT INTO Persons (TheDate)
VALUES
($datevar)";
but when it still doesn't work.

thanks in advance for all your help!
P.S for my own interest, when u save something as a timestamp database, and then u retrieve it and echo it, does it echo it as the actual time stamp (and u have to use the date() function to format it), or does it turn it into YYYY-MM-DD-HH:M:SS for you?
__________________
"If you say something interesting, people will remember your name" ~ Anonymous


Please login or register to view this content. Registration is FREE
.asp <- Irony
Zoltar1992 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-19-2009, 06:55 PM Re: Saving date to dbase
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
You are making your life complex for nothing...
It's not working because mysql doesn't know what exactly you are giving him as input.
If it's the date/time of the moment you want:
Code:
create table tst(
  tst timestamp,
  tdt datetime
);

insert into tst (tst, tdt) values (now(), now());
insert into tst (tst, tdt) values ('2028-10-25', '2034/12/24');
insert into tst (tst, tdt) values ('1978-10-25 18:03:25', '999/12/31 23:59:999');

select * from tst;
+---------------------+---------------------+
| tst                 | tdt                 |
+---------------------+---------------------+
| 2009-02-20 00:56:27 | 2009-02-20 00:56:27 | 
| 2028-10-25 00:00:00 | 2034-12-24 00:00:00 | 
| 1978-10-25 18:03:25 | 0000-00-00 00:00:00 | 
+---------------------+---------------------+
3 rows in set (0.00 sec)
It gives you an "human language" representation of it's internal format.
You can shape it your way using mysql's time and date functions like unix_timestamp() and date_format().
I recommand you to go that way, it's simplier and faster than using PHP date()
http://dev.mysql.com/doc/refman/5.0/...unix-timestamp
http://dev.mysql.com/doc/refman/5.0/...on_date-format
Code:
select unix_timestamp(tst), unix_timestamp(tdt) from tst;
+---------------------+---------------------+
| unix_timestamp(tst) | unix_timestamp(tdt) |
+---------------------+---------------------+
|          1235087787 |          1235087787 | 
|          1856037600 |          2050527600 | 
|           278183005 |                   0 | 
+---------------------+---------------------+
3 rows in set (0.00 sec)

select date_format(tst,'%d - %M - %Y'), date_format(tdt,'%r in the %j day of %Y') from tst;
+---------------------------------+-------------------------------------------+
| date_format(tst,'%d - %M - %Y') | date_format(tdt,'%r in the %j day of %Y') |
+---------------------------------+-------------------------------------------+
| 20 - February - 2009            | 12:56:27 AM in the 051 day of 2009        | 
| 25 - October - 2028             | 12:00:00 AM in the 358 day of 2034        | 
| 25 - October - 1978             | 12:00:00 AM in the 32212254 day of 0000   | 
+---------------------------------+-------------------------------------------+
3 rows in set (0.00 sec)
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 02-19-2009 at 07:03 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Saving date to dbase
 

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.18189 seconds with 12 queries