11 Replies - 871 Views - Last Post: 26 January 2012 - 09:30 PM Rate Topic: -----

Topic Sponsor:

#1 grydz3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 26-August 11

Inserting DATETIME format to a database

Posted 26 January 2012 - 04:43 AM

Greetings. again thanks for reading this . I have an input name:timestamp to get date and time that has a (28-1-2012 19:38:36): i think mysql accept format for datetime is YYYY-mm-dd so i made change to my var $eventdate to get that result .but still cant get the right input inside my database still gets

0000-00-00
00:00:00

can anyone tell me what am i doing wrong or lend me to the right directions. any tutorial will do . thanks guys. have a great day.
$eventdate = $_POST['timestamp'];

$eventdate = explode(" ",$_POST['timestamp']); // separtes date and time
        $date = $eventdate[0]; 
	$time = $eventdate[1];
	//dmy
	$dates = explode("-",$date);
	$d = $dates[0];
	$m = $dates[1];
	$y = $dates[2];
	
	$date = array($y,$m,$d);
	$eventdate = implode("-",$date)." ".$time;




Is This A Good Question/Topic? 0
  • +

Replies To: Inserting DATETIME format to a database

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 2127
  • View blog
  • Posts: 5,349
  • Joined: 08-June 10

Re: Inserting DATETIME format to a database

Posted 26 January 2012 - 05:19 AM

usually it's easier to use the pair strtotime()/date(). you would only have to test, whether strtotime parses your date string correctly (usually it does).
$mysql_date = date("Y-m-d", strtotime($_POST['timestamp']));

Was This Post Helpful? 4
  • +
  • -

#3 enjoibp3  Icon User is offline

  • New D.I.C Head

Reputation: 13
  • View blog
  • Posts: 41
  • Joined: 02-March 11

Re: Inserting DATETIME format to a database

Posted 26 January 2012 - 06:22 AM

If "28-1-2012 19:38:36" is what you data outputs as try adding a 0 in front of the month. so it would be:

<?php date('d-m-Y H:i:s'); ?>

Was This Post Helpful? 2
  • +
  • -

#4 xxxjj18  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 125
  • Joined: 30-November 11

Re: Inserting DATETIME format to a database

Posted 26 January 2012 - 06:40 AM

Both enjoibp3 and Dormilich are correct. A simpler way to insert the time into your DB though, would be to just have the date set as a varchar(255) type, and then to insert time() as its value whenever you add something to the DB. Then when you want to recall the date, you could simply do:

$newdate = date('d-m-Y H:i:s',$row["timestamp"]);

//Display the new date:

print $newdate;

//Would output something like:

24-01-2012 14:50:10



Was This Post Helpful? -1
  • +
  • -

#5 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 2127
  • View blog
  • Posts: 5,349
  • Joined: 08-June 10

Re: Inserting DATETIME format to a database

Posted 26 January 2012 - 06:44 AM

View Postxxxjj18, on 26 January 2012 - 02:40 PM, said:

A simpler way to insert the time into your DB though, would be to just have the date set as a varchar(255) type, and then to insert time() as its value whenever you add something to the DB.

WHAT? you can't honestly recommend using a string type for a date/time/datetime field. MySQL's date types are there so you can do date/time calculations within SQL (which don't work with varchar, btw).

besides that, this particular problem (inserting the current time) can be solved better in SQL:
CREATE TABLE test
(
    data VARCHAR(255) NOT NULL,
    created DATETIME DEFAULT CURRENT_TIMESTAMP
)

-- example insert
INSERT INTO test (data) VALUES (?)

-- get data of the last week
SELECT data FROM test WHERE created > DATE_SUB(CURRENT_TIMESTAMP INTERVAL 1 WEEK)

This post has been edited by Dormilich: 26 January 2012 - 06:54 AM

Was This Post Helpful? 4
  • +
  • -

#6 xxxjj18  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 125
  • Joined: 30-November 11

Re: Inserting DATETIME format to a database

Posted 26 January 2012 - 07:15 AM

I meant if the date was just a varchar type from the start (so that the field was never actually a timestamp), then he could insert the time string into that. Then when he wants to retrieve the date, he could select the time string and apply the date() method to it. Maybe this approach isn't the best :\

This post has been edited by xxxjj18: 26 January 2012 - 07:16 AM

Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 2127
  • View blog
  • Posts: 5,349
  • Joined: 08-June 10

Re: Inserting DATETIME format to a database

Posted 26 January 2012 - 07:33 AM

it's a "no-brainer" for SQL to convert a string into a date. additionally it saves space (date fields are smaller than varchar fields) and you can do calculations (e.g. strftime('%d. %m. %Y', created) AS `date`). that also makes the SQL queries in PHP way more simple.

This post has been edited by Dormilich: 26 January 2012 - 07:35 AM

Was This Post Helpful? 1
  • +
  • -

#8 grydz3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 26-August 11

Re: Inserting DATETIME format to a database

Posted 26 January 2012 - 09:05 AM

thanks for all the comments guys. i learned something again. i use xxxjj18 method. date('d-m-Y H:i:s',$row["timestamp"])and it works for me! and will study how timestamp really works .again thanks guys.
Was This Post Helpful? 0
  • +
  • -

#9 grydz3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 26-August 11

Re: Inserting DATETIME format to a database

Posted 26 January 2012 - 09:14 AM

oh wait guys now i got an error : A non well formed numeric value encountered in C:\wamp\www\reserve.php on line 91 :oops:
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 2127
  • View blog
  • Posts: 5,349
  • Joined: 08-June 10

Re: Inserting DATETIME format to a database

Posted 26 January 2012 - 11:39 AM

so what is line 91?
Was This Post Helpful? 0
  • +
  • -

#11 grydz3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 26-August 11

Re: Inserting DATETIME format to a database

Posted 26 January 2012 - 09:00 PM

is there a problem on the result i get from the datepicker made from javascript?
the result gets in my database but gets this error 'A non well formed numeric value encountered in '
$eventdate = date('Y-m-d H:i:s',$eventdate);

Was This Post Helpful? 0
  • +
  • -

#12 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1744
  • View blog
  • Posts: 2,667
  • Joined: 08-June 10

Re: Inserting DATETIME format to a database

Posted 26 January 2012 - 09:30 PM

What is the value of $eventdate before that line?

Like Dormilich did in his first post, you typically use the strtotime() function on user input to convert it into a Unix timestamp, to make it compatible with the date() function.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1