Display Date dd/mm/yy in php with time from date/time - php

I store the date and time in mysql as a date/time field which has this format: 2012-03-12 14:51:26, what i am trying to do is simply rearrange the DD/MM/YY to look like this.
When i use the following code, it just gives me a date wrong format warning.
echo date_format($date, 'Y-m-d H:i:s');

If you are just displaying the date you can supply a certain format in the SQL query
SELECT DATE_FORMAT("%d/%m/%Y", date_column) FROM table

If you convert the MySQL timestamp to a unix timestamp, then you can use the date() function to output it in whatever format you like:
$unixTimestamp = strtotime($mysqlDate);
echo date($dateFormat, $unixTimestamp);
See the date format strings here: http://php.net/manual/en/function.date.php

First, convert it to a Unix timestamp (which I find to be all around better than a date_time field for a lot of reasons), then use PHP's date function.
echo date('Y-m-d h:i:s', strtotime($date));

Simply do:
date("d/m/Y", strtotime($date));
And read about strtotime function.

this will work.
$date = date_create("2012-03-24 17:45:12");
echo date_format($date, 'Y-m-d H:i:s');

Related

Could not convert datetime format using PHP

I tried to use strtotime function to format the today's date in PHP but its giving me the wrong result. My code is given below.
<?php
$today = date("m-d-Y H:i:s");
echo date('m-d-Y H:i:s', strtotime($today));
?>
Here, I am getting this 01-01-1970 05:30:00 result.
Here, I need to get the proper datetime result.
date("m-d-Y") is what's causing issues for you. For example, take 01-02-2019 and 02-01-2019 - which is Februrary 1st and which is January 2nd? That format will make strtotime() return false, as it doesn't know what format that is for days that are greater than 12.
d-m-Y would be expected and a valid format.
You can use DateTime::createFromFormat() instead. Then you can create a valid DateTime object from that format, and use it however you need it to.
$today = DateTime::createFromFormat("m-d-Y H:i:s", date("m-d-Y H:i:s"));
echo $today->format("m-d-Y H:i:s");
Live demo
Documentation for DateTime::createFromFormat()
Alternatively, if you just need to print the date directly and not process it further, you don't need to go through any hoops and can just use date() as you were, without the second line. But you can not use that result in a strtotime() function, as it will return incorrect results.
echo date("m-d-Y H:i:s");

Example for specific date format

Hi this is my date format "Month/yyyy" please provide an example of
date for this format i want to parse date to this format
Use the strtotime() and date() functions:
http://php.net/manual/en/function.date.php
http://php.net/manual/en/function.strtotime.php
$date = '2018-06-07';
echo date('m/Y', strtotime($date));

PHP: Changing incorrect date format into correct format

I have MySQL database with field date format dd/mm/YYYY. As you know this date format is not working perfect with PHP.
How can I transform such (dd/mm/YYYY) into good-looking (YYYY-mm-dd). If there is a PHP function?
You can use the DateTime::createFromFormat method
$dateTime = DateTime::createFromFormat('d/m/Y', '25/02/2016');
echo $dateTime->format('Y-m-d');
You can format dates using PHP's date() function.
$date = "2016-03-21";
$newDate = date("Y-m-d", strtotime($date));
you can fetch date directly from query by using mysql function DATE_FORMAT(date_column,"%d-%m-%Y")
select DATE_FORMAT(date_column,"%d-%m-%Y") as date from table
this will return date in 01-02-2016 format

How to format `23/05/2012` into insertable value in DATETIME column?

I have dates formatted as d/m/y. How can I insert them into a DATETIME column?
Use MySQL's STR_TO_DATE() function:
INSERT INTO my_table VALUES (STR_TO_DATE('26/5/12', '%e/%c/%y'))
You need to use php's date() function along with strtotime() to convert date to any format you want.
MySQL database stores the date in YY-MM-DD format for datetime datatype, so if for example you have a date
$date = '26/05/2012';
You can convert it by using date() and strtotime()
$formatDate = date('Y-m-d', strtotime('26/05/2012'));
This will convert the date from 26/05/2012 to 2012-05-26 which then can be inserted into the database.
If you are using a timestamp datatype to store the date in your database, then all you need is to convert the current date into unix timestamp and store in database for example.
$date = strtotime('26/05/2012');
//this will convert the date to unix timestamp
Update:
as pointed out by #wallyk (thank you), strtotime() does not handles dd/mm/yy format. the fix is to replace the slash / by -m below code should work for you.
date('Y-m-d', strtotime(str_replace('/', '-', '26/05/2012')));
Try this:
$mysqldate = date("m/d/y g:i A", $datetime);
$date = date('d/m/Y');
$date = strtotime($date); //in unix time stamp format
Basically american date format is MM/DD/YYYY and you are providing DD/MM/YYYY so thats why startotime() returns you a null values on this input; and i prefer you must follow standard date format of american (MM/DD/YYYY) because if you are using mentioned format of date that will create more problems as well in different places ..
if you check by this
echo date('Y-m-d', strtotime('05/26/2012') );
and it is working fine ..
you could change your DATE column into a String Column and insert the data when ever you want 2 check if the date is right you can use a regular expression to do so

PHP datetimestamp formatting

I currently have a datetimestamp in the following format when posted from a form: dd/mm/yyyy hh:mm however I am trying to convert it to the appropriate format before MySQL insertion which is in the following format: yyyy-mm-dd hh:mm:ss. The ss seconds will always be 0.
Is it correct to use strtotime?
I think it's safer to use strptime, because of month-day-year / day-month-year inconsistence.
print_r(strptime($date_in_mdy_format, '%m/%d/%Y %H:%M'));
yes so you should use the following:
$form_date = $_POST['form_date'];
$date_insert = date('Y-m-d H:mm:0', strtotime($form_date));
Yes you can use strtotime, and that would be
$mysqldate = date('Y-m-d H:i:s', strtotime($mydate));
Then you put the $mysqldate in the SQL query.
Also see the manual .

Categories