Skip to content

Instantly share code, notes, and snippets.

@khoand0000
Last active October 22, 2015 18:33
Show Gist options
  • Save khoand0000/463323b8e48e2cff936d to your computer and use it in GitHub Desktop.
Save khoand0000/463323b8e48e2cff936d to your computer and use it in GitHub Desktop.
var d = new Date();
var mysqlDate = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds(); // 2001-03-10 17:16:18 (the MySQL DATETIME format)
// remember: date() return time in UTC (GMT+00)
$mysqldate = date("Y-m-d H:i:s"); // 2001-03-10 17:16:18 (the MySQL DATETIME format)
$phpdate = strtotime( $mysqldate ); // convert mysql datetime format to date (php)
$mysqldate = date( 'Y-m-d H:i:s', $phpdate ); // convert date (php) to mysql datetime format
$mysqldate = \Carbon\Carbon::createFromTimeStamp(time())->toDateTimeString() // equivalent, using Carbon
// using Carbon, convert "28/10/2015 23:59:59" (timezone GMT+07) to timestamp (UTC: GMT+00)
$expired_date = \Carbon\Carbon::createFromFormat('d/m/Y H:i:s', "28/10/2015 23:59:59", 'GMT+07'); // return 2015-10-28 23:59:59
echo date('c', $expired_date->timestamp); // 2015-10-28T16:59:59+00:00
// using Carbon, convert "28/10/2015" (timezone GMT+07) to timestamp (UTC: GMT+00), note: time is current (ex: 01:10:19 GMT+07)
$expired_date = \Carbon\Carbon::createFromFormat('d/m/Y', "28/10/2015", 'GMT+07'); // return 2015-10-28 01:10:19
echo date('c', $expired_date->timestamp); // 2015-10-27T18:10:19+00:00 (note: 27)
// using Carbon,convert "28/10/2015" without defining timezone to timestamp (UTC: GMT+00), note: time is current (ex: 01:10:19 GMT+07)
$expired_date = \Carbon\Carbon::createFromFormat('d/m/Y', "28/10/2015"); // return 2015-10-28 18:10:19
echo date('c', $expired_date->timestamp); // 2015-10-28T18:10:19+00:00 (note: 28)
mysql> show variables like '%time_zone%';
+------------------+---------------------+
| Variable_name | Value |
+------------------+---------------------+
| system_time_zone | India Standard Time |
| time_zone | Asia/Calcutta |
+------------------+---------------------+
mysql> create table datedemo(
-> mydatetime datetime,
-> mytimestamp timestamp
-> );
mysql> insert into datedemo values ((now()),(now()));
Use DATETIME instead of TIMESTAMP
ref: http://stackoverflow.com/questions/409286/datetime-vs-timestamp
mysql> select * from datedemo;
+---------------------+---------------------+
| mydatetime | mytimestamp |
+---------------------+---------------------+
| 2011-08-21 14:11:09 | 2011-08-21 14:11:09 |
+---------------------+---------------------+
mysql> set time_zone="america/new_york";
mysql> select * from datedemo;
+---------------------+---------------------+
| mydatetime | mytimestamp |
+---------------------+---------------------+
| 2011-08-21 14:11:09 | 2011-08-21 04:41:09 |
+---------------------+---------------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment