PHP and MySQL syntax: Difference between revisions
Jump to navigation
Jump to search
(→now) |
|||
| Line 2: | Line 2: | ||
== timestamp == | == timestamp == | ||
=== now === | === now === | ||
the human readable time format ex: 2010-12-21 10:05:06 | the '''human readable''' time format ex: 2010-12-21 10:05:06 | ||
* php: echo date("y-m-d H:i:s", time() ); //Convert the time stamp of current time to the human readable time format. Ex: return '2010-12-21 10:05:06' | * php: echo date("y-m-d H:i:s", time() ); //Convert the time stamp of current time to the human readable time format. Ex: return '2010-12-21 10:05:06' | ||
* mysql: | * mysql: | ||
| Line 9: | Line 9: | ||
** SELECT CURRENT_TIMESTAMP; | ** SELECT CURRENT_TIMESTAMP; | ||
the current timestamp ex: 1292897201 | the current '''timestamp''' ex: 1292897201 | ||
* php: echo time(); | * php: echo time(); | ||
* mysql: SELECT UNIX_TIMESTAMP(); //2011-05-30 01:56:38 returns 1306720622 | * mysql: SELECT UNIX_TIMESTAMP(); //2011-05-30 01:56:38 returns 1306720622 | ||
* javascript: [http://stackoverflow.com/questions/221294/how-do-you-get-a-timestamp-in-javascript datetime - How do you get a timestamp in JavaScript? - Stack Overflow] {{access | date = 20110323}} | * javascript: [http://stackoverflow.com/questions/221294/how-do-you-get-a-timestamp-in-javascript datetime - How do you get a timestamp in JavaScript? - Stack Overflow] {{access | date = 20110323}} | ||
compare the timestamp and human readable time format | |||
SELECT UNIX_TIMESTAMP( ) , FROM_UNIXTIME( UNIX_TIMESTAMP( ) , '%Y-%m-%d %H:%i:%S' ); | |||
=== specified time === | === specified time === | ||
Revision as of 16:54, 8 June 2011
Convert time stamp <--> the human readable time format
timestamp
now
the human readable time format ex: 2010-12-21 10:05:06
- php: echo date("y-m-d H:i:s", time() ); //Convert the time stamp of current time to the human readable time format. Ex: return '2010-12-21 10:05:06'
- mysql:
- SELECT NOW(); /*return the human readable current time: 2011-04-01 12:19:43 */
- SELECT NOW()+0; //2011-04-01 12:19:43 returns 20110401122023.000000
- SELECT CURRENT_TIMESTAMP;
the current timestamp ex: 1292897201
- php: echo time();
- mysql: SELECT UNIX_TIMESTAMP(); //2011-05-30 01:56:38 returns 1306720622
- javascript: datetime - How do you get a timestamp in JavaScript? - Stack Overflow [Last visited: 20110323]
compare the timestamp and human readable time format
SELECT UNIX_TIMESTAMP( ) , FROM_UNIXTIME( UNIX_TIMESTAMP( ) , '%Y-%m-%d %H:%i:%S' );
specified time
the human readable time format ex: 2010-12-21 10:05:06
- php: echo date("y-m-d H:i:s", 1292897201 ); // convert the time stamp 1292897201 to the human readable time format
- mysql:
- SELECT FROM_UNIXTIME( 1306311155 ); //convert the time stamp 1306311155 to the human readable time format 2011-05-25 08:12:11
- SELECT FROM_UNIXTIME( 1306311155, '%Y-%m-%d %H:%i:%S' ); //convert the time stamp 1306311155 to the human readable time format 2011-05-25 08:12:11
convert human-readable time to timestamp
- php:strtotime()
- mysql: SELECT UNIX_TIMESTAMP('2011-03-15 18:53:57'); /* return timestamp: 1300186437 */
time difference of two time values
- php: using mktime() function
- mysql: SELECT TIMEDIFF('2010:01:01 00:00:00', '2010:02:01 00:00:00'); /* return -744:00:00 */
- PHP Date and Time functions
- MySQL The DATETIME, DATE, and TIMESTAMP Types
- MySQL Date and Time Functions
before delete trigger to make backup of deleted data
MySQL :: Re: before delete trigger to make backup of deleted data
step 1: create table hello_deleted_data with the same structure of table hello
step 2: add a trigger
create trigger hello before delete on hello for each row insert into hello_deleted_data (x,y,z) values (OLD.x, OLD.y, OLD.z);
tested on version: mysqlnd 5.0.7-dev - 091210