• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

php and mysql

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

kraven

Member
Joined
Nov 11, 2004
I'm simply trying to get a current date that's compatible with mysql using PHP and can't understand the resources online.

Can someone dumb this down for me?

I've tried:
Code:
$today = getdate();
as that seemed simplest.
 
Just a quick check gave me that it returns an array of date information. I would honestly give google one more try now that you've slept, and if you dont get an answer in minutes then just write your own custom formatting routine that you can swap out later on when you figure it out later on. (I do this all the time).

Remember, most date formats in code are really just longs. Usually the number of milliseconds since some other date and time. Displaying it as month, day, year is an internal set of routines. But the data itself is just a long. MySQL likes it basically as a string. So just write something to get it working for now.

My opinion.
 
I'm simply trying to get a current date that's compatible with mysql using PHP and can't understand the resources online.

Can someone dumb this down for me?

I've tried:
Code:
$today = getdate();
as that seemed simplest.

There are number of ways of handling dates in PHP and MySQL. If you had specified that you already had a particular type you wanted to use, I'd work with that. As you just seem to want a working solution of some sort, I'll give you the following:

Code:
CREATE TABLE table1 (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    added BIGINT UNSIGNED NOT NULL,
    data VARCHAR(100) NOT NULL
);

The above creates a really simple table with three fields. An ID field (because you almost never want a situation where there isn't a primary key of some sort); an 'added' field which is going to be your date-time; and a "data" field which could be names, or move titles or whatever it is the table is supposed to be storing. Don't worry about the Unsigned, Not Nulls, etc. That's just me showing good practice - it's not vital.

Code:
<?php
mysql_connect('hostname', 'username', 'password');
mysql_select_db('db_name');

$data = 'Here is some data. It\\\'s good';
$time = time(); //This gives a UNIX Timestamp - one way of handling dates.

mysql_query("INSERT INTO table1 (added, data) VALUES ($time, '$data');");
?>

Now what the above does is add a row to the table created above. The important part for your question is that the "added" field in your database is actually just a number - an integer. It's literally a number of seconds since 1970. But you don't need to worry about that.

To get your data back again, you can do this:

Code:
<?php
mysql_connect('hostname', 'username', 'password');
mysql_select_db('db_name');

$result = mysql_query('SELECT * FROM table1');
while($record = mysql_fetch_assoc($result)) {
    echo($record['added'] . "\n"); //This prints out the raw time.
    echo(date('Y-m-d',$record['added']) . "\n");  //This prints out something humans can read
}
?>

There's one thing missing from the above, which is that I've used the time() function which returns the value right now. If you want to get a particular time, you can do:

Code:
<?php
$added = strtotime('31-10-2012 23:55');
?>

That's one of the main ways of handling dates with PHP and MySQL. It's the way that I like to do it. Time stamps (i.e. a number of seconds) are clear, unambiguous and space-efficient.

But there is also another major way, which is to use the DATETIME field when you create your database table. In that case, instead of having to convert to seconds and back, you could just use:

Code:
$added = '2012-10-31 23:58:00';

That would work too, if you'd used a DATETIME type for 'added' in the database. I didn't show you that one, because both approaches have plusses and minuses and I didn't want to confuse you by showing several different ways of doing things. If you already have your database created and it has a DATETIME field, then all you need to do is use the above instead of $added = time(); .

Does this help?

EDIT: There's a lot I could talk about in the above. For example, I wouldn't recommend connecting to the database this way, but it's just a quick way so I can show you the date-time stuff you asked about.
 
Last edited:
Code:
$today = date('Y-m-d');
// or
$now = date('Y-m-d H:i:s');
 
Last edited:
holy response batman.

our DB is using DATETIME for events.

I'll try your guys' code, thanks.
 
Back