Jump to content

MySQL timestamp with php


amalosoul

Recommended Posts

I prefer to let the database handle things such as this.

SELECT DATE_FORMAT([i]timestamp[/i], '%e') AS DayFromTS FROM [i]table[/i] WHERE 1

[i]timestamp[/i] is the name of the column containing the timestamp value, '%e' is the format to return (Day of the month, numeric (0..31)), DayFromTS is the name that it will return it as.

MySQL is a very, very powerful langugae.  Before you go off writing bits of PHP code to accomplish some tasks, I would look around in the MySQL manual at the [b]Functions and Operators[/b] found here:
http://dev.mysql.com/doc/refman/4.1/en/functions.html
MySQL timestamps are formatted differently depending on the server version and mode, so you have to bear that in mind.

You could use the SQL function DAY(), as in

[code]
SELECT DAY(timestamp_field) FROM table
[/code]

or if you need something for PHP and you're certain that's the format of your field (and not YYYY-MM-DD HH:MM:SS ?) you could just do

[code]
$day = substr( $timestamp, 6, 2 ); // where format is YYYYMMDDHHMMSS
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.