This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
Hello i am trying to convert this date.
01. 06. 2015
Into mysql format, so i can insert it into database.
I tried this code, and other ombination with date and date format function.
`$mysqldate = date( 'Y-m-d H:i:s', strtotime($datefrom));`
but i get result
1.1.1970
Is it possible to do this with some date function, or i must use regex for resolving this problem. Maybe dateformat function?
Use this.. works fine
$datefrom = "01. 06. 2015";
$datefrom = str_replace( ". ", "-", $datefrom);
$mysqldate = date( 'Y-m-d H:i:s', strtotime($datefrom));
you need to clean your dateformat
<?php
$date = '01. 06. 2015';
$date = str_replace(' ', '', $date); // replace whitespaces
$mysqldate = date( 'Y-m-d H:i:s', strtotime($date));
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I have a date time in string with format Y-m-d H:i:s like this:
$dateTime = '2018-07-06 18:53:21';
i want to conver it into dmy format, like this:
$convertedDateTime = $this->convertDateTime($dateTime);
echo $convertedDateTime;
and the result i expected from above echo is 060718, how can i achieve this?
You can use date_format like this:
echo date_format($date,"Y/m/d H:i:s"); //e.g. 2013/03/15 00:00:00
echo date_format($date,"dmy"); //e.g. 150313 -- Y capital would return 2013
there's two methods to use:
date - https://secure.php.net/manual/en/function.date.php
usage example:
$myDate = '2018-07-06 09:49:00';
$myDate = date('d-m-y', strtotime($myDate));
but more often than not you'll want to use DateTime https://secure.php.net/manual/en/class.datetime.php:
$myDate = DateTime::createFromFormat('Y-m-d H:i:s', '2018-07-06 09:49:00');
$$newDate = $myDate->format('d-m-Y');
\DateTime::createFromFormat('Y-m-d H:i:s', '2018-07-06 18:53:21')->format('dmy');
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have code:
$date= "2016/Apr/16";
$time="5:00 PM";
I want to convert it to DB format like 2016-04-16 17:00:00 in php .
I am not getting exact code from anywhere.
This way will solve your problem.
<?php
$date= "2016/Apr/16";
$time="5:00 PM";
$fullDate = str_replace('/','-',$date) . " " . $time;
$newDate = date("Y-m-d H:i:s", strtotime($fullDate));
echo $newDate;
?>
Result
More Examples;
http://www.w3schools.com/php/func_date_strtotime.asp
Convert date format yyyy-mm-dd => dd-mm-yyyy
Normally you would use the following code:
date('Y-m-d H:i:s', strtotime($date.' '.$time));
If it returns the year 1970, then the input date (the one from $date variable) is not formatted according to a standard that can be recognised by php.
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
So far I'm doing this to convert dates
$date = '01/1990';
date('Y-m-d', strtotime(str_replace('-', '/', $date . '/01')));
But I get back a boolean false; Any ideas?
Your str_replace is searching for - and replacing with /.
Here is a simplified solution that opts to not concatenate the day onto the date string.
$date = '01/1990';
$date = str_replace('/', '-', $date);
$date = date('Y-m-1', strtotime($date));
If you do want to concatenate your day onto the date string format as dd/mm/yyyy
$date = '01/1990';
$date = '01/' . $date;
$date = str_replace('/', '-', $date);
$date = date('Y-m-d', strtotime($date));
Replace $data with $date:
echo date('Y-m-d', strtotime(date('Y-d-m', strtotime('01/' . str_replace('-', '/', $date)))));
strtotime accepts dd-mm-yyyy and mm/dd/yyyy (and some other formats like yyyy-mm-dd). The only way to differentiate between dd-mm-yyyy and mm/dd/yyyy is the separator.
Since you start with a string that appears to be in mm/yyyy format, the easiest solution would be to convert it to dd-mm-yyyy:
$date = '01/1990';
date('Y-m-d', strtotime('01-'.str_replace('/', '-', $date)));
And you also might want to check the DateTime class.
This question already has an answer here:
MySQL -PHP insert DATETIME
(1 answer)
Closed 10 years ago.
In PHP, how can I convert a date/time string in the format 20:13-02-05T13:45:17-0500 to a valid MySQL datetime so I can store it?
So far I've tried
$date_id = date('Y-m-d H:i:s', strtotime($date_id));
I think this should work:
$date_to_store = '20:13-02-05T13:45:17-0500';
$date_for_mysql = date( 'Y-m-d H:i:s' , strtotime( $date_to_store ) );
Then in your query, just insert the $date_for_mysql:
mysql_query("INSERT INTO `table` (`timestamp`) VALUES ('" . $date_for_mysql . "')");
Of course you should be using prepared statements, but this is just to illustrate the date conversion.
// convert non-standard year format 20:13 to 2013
$date = preg_replace ('/^(\d{2}):(\d{2})/', '$1$2', $date_id);
$timestamp = strtotime ($date);
echo date ('Y-m-d H:i:s', $timestamp);
explode(your_time);
This should get you the time chunks, and then use date and mktime.
date(Your_preferred_format, mktime(chunks_as_per_mktime));
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Getting time and date from timestamp with php
I am trying to convert this date format:
09/14/2012 10:11
into this other standard timestamp format:
2012-09-14 09:44:54
What is the most efficient way of doing it?
You can use PHP's date and strtotime functions:
$date = '09/14/2012 10:11';
$formatted = date('Y-m-d H:i:s', strtotime($date));
You can use DateTime object
$date = DateTime($myDate);
or your custom input format with createFromFormat
$date = DateTime::createFromFormat('d/m/y H:i:s', $myDate);
format Will make your output
$result = $date->format('Y-m-d H:i:s');
date('Y-m-d H:i:s', strtotime('09/14/2012 10:11'));
Though it's going to be hard to change the time.
Convert first into timestamp with strtotime and convert timestamp into date with date.
$outputDate = date( 'Y-m-d H:i:s', strtotime( $inputDate ));