I have a database that people post to and the posts get inserted.
I want to log the date and time in the database too, using the DATETIME column type. How would I do this using the date() function?
From mysql docs
The DATETIME type is used for values that contain both date and time
parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD
HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to
'9999-12-31 23:59:59'.
So in PHP you just need to use the date function
$datetime = date('Y-m-d H:i:s');
echo datetime; // display example: 2014-01-27 17:21:34
Different Ways to format Date time in php
<?php
#The two example outputs are based on this time
#eg1: 2010-06-19 15:09:35
#eg2: 2010-06-19 08:30:59
echo date("Y-m-d");
#Output eg1: 2010-06-19
#Output eg2: 2010-06-19
echo date("n/j/y");
#Output eg1: 6/19/10
#Output eg2: 6/19/10
echo date("Y-m-d H:i:s");
#Output eg1: 2010-06-19 15:09:35
#Output eg2: 2010-06-19 08:30:59
echo date("l, jS F Y h:i a");
#Output eg1: Saturday, 19th June 2010 03:09 pm
#Output eg2: Saturday, 19th June 2010 08:30 am
echo date("jS M y g:i A");
#Output eg1: 19th Jun 10 3:09 PM
#Output eg2: 19th Jun 10 8:30 AM
echo date("D, j M Y G:i:s");
#Output eg1: Sat, 19 Jun 2010 15:09:35
#Output eg2: Sat, 19 Jun 2010 8:30:59
Credit to nazly
Doing this can address your concern:
date('Y-m-d H:i:s');
You can also set your DATETIME column to have a default CURRENT_TIMESTAMP.
Here:
$my_date = date("Y-m-d H:i:s");
INSERT INTO my_table (date_time) VALUES ('$my_date');
php DateTime
If you want to insert the current time, you don't need PHP date() (and figure out the proper format string) - just use MySQL NOW():
INSERT INTO mytable (insert_time) VALUES (NOW());
Related
I have a query that inserts date in this format
$time = date("m-d-y");
But when I Fetch and output date it shows wrong date. real: October 2020 but outputs: January 1970
$time = $row1['time'];
$newDate = date('F Y', strtotime($time));
echo $newDate;
How do I output date in this format: 20 OCT, 2020
Try not to store date/time using varchar datatype. If you can change it, please do. However, if you can't change database structure, you can use date_create_from_format() to create a DateTime object from a custom format:
echo date_create_from_format('m-d-y', "10-29-20")->format('F Y');
Output:
October 2020
Edit: Change the format part to ->format('d M, Y') to match your desired format
Output:
29 Oct, 2020
I have a field within a MySQL database that has a date format of the following:
Mon, 17 Aug 2015 19:14:22 +0100
I want to display this date using PHP in the following format:
YYYY-MM-DD HH:MM:SS
I have no idea how to pick out the different elements and reformat them.
Can somebody help?
Many thanks,
John
You can try these :-
$date = "Mon, 17 Aug 2015 19:14:22 +0100";
$newDate = date('Y-m-d H:i:s', strtotime($date));
echo $newDate;
You can do it in mysql
SELECT
DATE_FORMAT(test.dateFrom, '%Y-%M-%d %H:%i:%s') as date,
FROM test
Or in php
$date = date('Y-m-d H:i:s', strtotime($datefrommysql) );
I have a php script that returns the date of the server:
<?php
echo date('D, d M y H:i:s a');
?>
but when I print this value on the client site I get:
Tue, 29 Sep 15 16:19:28 pm
But instead I need the date in this format:
Tue Sep 29 2015 16:18:00 GMT+0200 (Central Europe Daylight Time)
How should I modify my php script then to have it like this?
Thanks!
echo (new DateTime())->format('r');
$datestring = "26-08-2015 03:35:28"; //date as string
$date = new DateTime($datestring); //String to datetime conversion
$date = $date->format('D d M y H:i:s O e'); //format the date
echo $date;
This is the manual link. Timezone you have to set.
Output will look like Wed 26 Aug 15 03:35:28 +0200 Europe/Paris
Go to the very bottom of this page http://php.net/manual/en/function.date-default-timezone-set.php
Does anyone could help me how to convert data from email header?
I have the next date format from email header:
Wed, 28 Apr 2010 21:59:49 -0400
I need to convert them into mysql Date, or timestamp. Thanks!
You should be using DateTime for this, specifically DateTime::createFromFormat():
$str = 'Wed, 28 Apr 2010 21:59:49 -0400';
$date = DateTime::createFromFormat( 'D, d M Y H:i:s O', $str);
Now, you have a Date object in $date, and you can grab the unix timestamp (if that's what you want), or you can format it into a date for MySQL.
echo $date->getTimestamp(); // Outputs: 1272506389
echo $date->format( 'Y-m-d H:i:s'); // For MySQL column, 2010-04-28 21:59:49
You can see it working in the demo.
I'm getting xml and rss feeds and putting the data into a database. I've run into two different date formats so far...
Wed, 21 Jul 2010 00:28:50 GMT
And
2010-07-20T17:33:19Z
I'm sure there will be more. My postgresql database for the date is timestamp without time zone. Is there an existing function in php or is there a procedure to convert the any date strings to timestamp without time zone (Y-m-d H:i:s)?
Use date with strtotime:
$date = date('Y-m-d H:i:s', strtotime('Wed, 21 Jul 2010 00:28:50 GMT'));
echo $date;
Result:
2010-07-21 05:28:50
.
$date = date('Y-m-d H:i:s', strtotime('2010-07-20T17:33:19Z'));
echo $date;
Result:
2010-07-20 22:33:19
You don't need to convert it at all. PostgreSQL should convert automatically:
postgres=# create table test_tz (f1 timestamp without time zone);
CREATE TABLE
postgres=# insert into test_tz (f1) values ('Wed, 21 Jul 2010 00:28:50 GMT');
INSERT 0 1
postgres=# insert into test_tz (f1) values ('2010-07-20T17:33:19Z');
INSERT 0 1
postgres=# select f1 from test_tz;
f1
---------------------
2010-07-21 00:28:50
2010-07-20 17:33:19
Timestamps are considered to be UTC.
$dt = new DateTime('Wed, 21 Jul 2010 00:28:50 GMT');
echo $dt->format('U'); // 1279672130
is the same timestamp as
$dt = new DateTime('Wed, 21 Jul 2010 02:28:50 CEST');
echo $dt->format('U'); // 1279672130
Note that the U formatting option requires PHP5.3 though. When supplying a timezone identifier in the Date String, the DateTime object recognizes the Timezone, so when you call the following on the GMT DateTime instance
echo $dt->format('Y-m-d H:i:s');
it will return 2010-07-21 00:28:50. You can change a DateTime object's timezone with it's setTimezone() method though.
$dt = new DateTime('Wed, 21 Jul 2010 02:28:50 GMT+2');
$dt->setTimezone(new DateTimeZone('UTC'));
echo $dt->format('Y-m-d H:i:s'); // 2010-07-21 00:28:50
But if you just need the timestamp, it's not needed.