How can I convert date which is in dd/mm/yy format to sql insert date format.
I tried
$time = new \DateTime('d/m/y H:i', $this->request->data['date_from']);
pr($time);
getting error.
DateTime::__construct() expects parameter 2 to be DateTimeZone, string
given
When I try
DateTime::createFromFormat('d/m/y H:i', $str)->format('Y-m-d')
Error: Class 'App\Controller\DateTime' not found
How can I do that
this works for me $myDate = date('Y-m-d', strtotime($this->request->data['date_from']))
For Cakephp 3.2 and above:
use Cake\I18n\Date;
$date = new Date($this->request->data['date_from']);
echo $date->format('Y-m-d');
Reference: https://book.cakephp.org/3.0/en/core-libraries/time.html#dates
I am parsing a feed which returns the date in form:
20150129180000
which should be [yyyy][mm][dd][hours][minutes][seconds]
I tried formatting this with the php date() function:
date('Y-m-d H:i:s', $event['start-dt'])
and got this:
640502-03-05 12:33:20
which is obviously not right. Is there a smooth way of converting this, or do I simply need to splice the string manually?
Doc
$date = DateTime::createFromFormat('YmdHis', '20150129180000');
echo $date->format('Y-m-d H:i:s');
You can use preg_replace to format the string
$date_str = preg_replace("/^(\d){4}(\d){2}(\d){2}(\d){2}(\d){2}(\d){2}/","$1-$2-$3 $4:$5:$6",$event["start-dt"]);
date('Y-m-d H:i:s', strtotime($date_str));
Or use php's DateTime object
$date = new DateTime($date_str);
Or better yet, use the DataTime createFromFormat method
$format = "YmdGis";
$dateObj = DateTime::createFromFormat ( $format, $event['start-dt'] );
Then you can use getTimestamp to retrieve the timestamp for calculations, if you need it.
The format is somewhat confusing compared to date until you are used to it, but this corresponds to your date string:
$date = DateTime::createFromFormat('YmdGis', '20150129180000');
echo $date->format('Y-m-d H:i:s');
Struggling without much success to turn strings like "16/Sep/2014 08:34" extracted from an array with explode command to unix timestamp like "2014-09-17 05:32:05" in PHP. Any help, please?
Edit: With #Erik's help I finally got the right result:
$date = DateTime::createFromFormat("d/M/Y H:i", $line[0]);
$date = $date->format('Y-M-d H:i');
$timestamp = strtotime($date);
You'll need to use DateTime::createFromFormat and then convert the resulting datetime to a timestamp by using $datetime->getTimestamp();
--
// this will create a generic PHP date object, which you can then manipulate into anything you want
$date = DateTime::createFromFormat( "d/M/Y H:i", "16/Sep/2014 08:34" );
// this will generate a unix timestamp (which is an integer)
$timestamp = $date->getTimestamp();
// this will generate the string you request in your question
$string = $date->format( "Y-m-d H:i:s" );
--
For more info on formatting dates, check out the PHP documentation: http://php.net/manual/en/datetime.createfromformat.php
I have a table with column date set to datetime. When i return and get the date from row it is only returning the date and not the time.
$date = $row["date"];
I have tried to format as below and I get the error of:
Warning: date_format() expects parameter 1 to be DateTime, string given
$date = date_format($row["date"], 'Y-m-d H:i:s');
How do I get the whole value?
in your select statement, cast the date into datetime. ex
SELECT CAST(date AS DATETIME) newDate
and retrieve it as
$dateTime = strtotime($row["newDate"]);
try:
$date = date_format(new DateTime($row['date']), "Y-m-d H:i:s");
OR
$date = date('Y-m-d H:i:s', strtotime($row['date']));
You need to convert your string $date to the right type. I had to try a few things to get this right, but this now behaves on my machine:
$thisDate = "2013-02-02 22:17:06"; // example you gave, as a string
$timezone="America/New_York"; // machine was complaining when I didn't specify
$DT = new DateTime($thisDate, new DateTimeZone($timezone)); // this really is a DateTime object
echo $DT->format('Y-m-d H:i'); // you can echo this to the output
$dateString = $DT->format('Y-m-d H:i:s'); // or format it into a string variable
You need to convert the string to date type first. Then date_format() will work. Try the following.
$date = date_format(date_create($row["date"]), 'Y-m-d H:i:s');
Good Luck
I have a datetime column in MySQL.
How can I convert it to the display as mm/dd/yy H:M (AM/PM) using PHP?
If you're looking for a way to normalize a date into MySQL format, use the following
$phpdate = strtotime( $mysqldate );
$mysqldate = date( 'Y-m-d H:i:s', $phpdate );
The line $phpdate = strtotime( $mysqldate ) accepts a string and performs a series of heuristics to turn that string into a unix timestamp.
The line $mysqldate = date( 'Y-m-d H:i:s', $phpdate ) uses that timestamp and PHP's date function to turn that timestamp back into MySQL's standard date format.
(Editor Note: This answer is here because of an original question with confusing wording, and the general Google usefulness this answer provided even if it didnt' directly answer the question that now exists)
To convert a date retrieved from MySQL into the format requested (mm/dd/yy H:M (AM/PM)):
// $datetime is something like: 2014-01-31 13:05:59
$time = strtotime($datetimeFromMysql);
$myFormatForView = date("m/d/y g:i A", $time);
// $myFormatForView is something like: 01/31/14 1:05 PM
Refer to the PHP date formatting options to adjust the format.
If you are using PHP 5, you can also try
$oDate = new DateTime($row->createdate);
$sDate = $oDate->format("Y-m-d H:i:s");
$valid_date = date( 'm/d/y g:i A', strtotime($date));
Reference: http://php.net/manual/en/function.date.php
Finally the right solution for PHP 5.3 and above:
(added optional Timezone to the Example like mentioned in the comments)
without time zone:
$date = \DateTime::createFromFormat('Y-m-d H:i:s', $mysql_source_date);
echo $date->format('m/d/y h:i a');
with time zone:
$date = \DateTime::createFromFormat('Y-m-d H:i:s', $mysql_source_date, new \DateTimeZone('UTC'));
$date->setTimezone(new \DateTimeZone('Europe/Berlin'));
echo $date->format('m/d/y h:i a');
An easier way would be to format the date directly in the MySQL query, instead of PHP. See the MySQL manual entry for DATE_FORMAT.
If you'd rather do it in PHP, then you need the date function, but you'll have to convert your database value into a timestamp first.
Forget all. Just use:
$date = date("Y-m-d H:i:s",strtotime(str_replace('/','-',$date)))
To correctly format a DateTime object in PHP for storing in MySQL use the standardised format that MySQL uses, which is ISO 8601.
PHP has had this format stored as a constant since version 5.1.1, and I highly recommend using it rather than manually typing the string each time.
$dtNow = new DateTime();
$mysqlDateTime = $dtNow->format(DateTime::ISO8601);
This, and a list of other PHP DateTime constants are available at http://php.net/manual/en/class.datetime.php#datetime.constants.types
This should format a field in an SQL query:
SELECT DATE_FORMAT( `fieldname` , '%d-%m-%Y' ) FROM tablename
Use the date function:
<?php
echo date("m/d/y g:i (A)", $DB_Date_Field);
?>
Depending on your MySQL datetime configuration. Typically: 2011-12-31 07:55:13 format. This very simple function should do the magic:
function datetime()
{
return date( 'Y-m-d H:i:s', time());
}
echo datetime(); // display example: 2011-12-31 07:55:13
Or a bit more advance to match the question.
function datetime($date_string = false)
{
if (!$date_string)
{
$date_string = time();
}
return date("Y-m-d H:i:s", strtotime($date_string));
}
SELECT
DATE_FORMAT(demo.dateFrom, '%e.%M.%Y') as dateFrom,
DATE_FORMAT(demo.dateUntil, '%e.%M.%Y') as dateUntil
FROM demo
If you dont want to change every function in your PHP code, to show the expected date format, change it at the source - your database.
It is important to name the rows with the as operator as in the example above (as dateFrom, as dateUntil). The names you write there are the names, the rows will be called in your result.
The output of this example will be
[Day of the month, numeric (0..31)].[Month name (January..December)].[Year, numeric, four digits]
Example: 5.August.2015
Change the dots with the separator of choice and check the DATE_FORMAT(date,format) function for more date formats.
You can also have your query return the time as a Unix timestamp. That would get rid of the need to call strtotime() and make things a bit less intensive on the PHP side...
select UNIX_TIMESTAMP(timsstamp) as unixtime from the_table where id = 1234;
Then in PHP just use the date() function to format it whichever way you'd like.
<?php
echo date('l jS \of F Y h:i:s A', $row->unixtime);
?>
or
<?php
echo date('F j, Y, g:i a', $row->unixtime);
?>
I like this approach as opposed to using MySQL's DATE_FORMAT function, because it allows you to reuse the same query to grab the data and allows you to alter the formatting in PHP.
It's annoying to have two different queries just to change the way the date looks in the UI.
You can have trouble with dates not returned in Unix Timestamp, so this works for me...
return date("F j, Y g:i a", strtotime(substr($datestring, 0, 15)))
This will work...
echo date('m/d/y H:i (A)',strtotime($data_from_mysql));
Using PHP version 4.4.9 & MySQL 5.0, this worked for me:
$oDate = strtotime($row['PubDate']);
$sDate = date("m/d/y",$oDate);
echo $sDate
PubDate is the column in MySQL.
Direct output e.g. in German format:
echo(date('d.m.Y H:i:s', strtotime($row["date_added"])));
$date = "'".date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $_POST['date'])))."'";