I'm trying to format a date in the form mm-dd-yyyy to the form yyyy-mm-dd, however, when I try formatting it, it comes out as 1969-12-31.
Here's my code:
$custom_date = "10-13-2013";
$formatted_date = date("Y-m-d", strtotime($custom_date));
What's wrong?
$custom_date = "10-13-2013";
$formatted_date = DateTime::createFromFormat("m-d-Y", $custom_date)->format("Y-m-d");
mm-dd-yyyy is not a format that is recognised by strtotime. That's because it wouldn't reliably be able to handle dates like 03-04-2013, it is the fourth of March or the third of April?
You need to parse it manually, or use the DateTime class.
list($m,$d,$y) = explode("-",$_GET['date']);
$timestamp = mktime(0,0,0,$m,$d,$y);
$formatted_date = date("Y-m-d",$timestamp);
Related
In my CodeIgniter application, I am getting date in different formats, such as: April 1st 2017, May 29, 2015, Jun-15-2015, 10-September-2015 and sometimes even with extra string such as Start: April 1, 2017. However, I want to convert the input date from any format to Y-m-d in order to save it in MySQL database. For example, if input date is April 1st 2017 I should get 2017-04-01. I have used below posted code for that but it is not working for all of the above mentioned cases. So please tell how can I write general conversion logic that can convert date from any format even if date has extra string with it (as mentioned above) to Y-m-d format.
Code:
$date = DateTime::createFromFormat('F jS Y', $old_date);
$new_date = $date->format('Y-m-d');
try this
$old_date = 'Jun-15-2015';
echo $newDate = date("Y-m-d", strtotime($old_date));
You can replace the extra string like
$date = str_replace('Start: ',' ',$date);
And after you can use date function of php
echo $date = date('Y-m-d',strtotime($date));
This might help even for inserting this date format into database tables
function convertUTCCombinedToLocal($utcDateTime) {
$utcDateTime = explode(" ", $utcDateTime);
$date = explode("-", $utcDateTime[0]);
$time = explode(":", $utcDateTime[1]);
$localDate = new DateTime();
$localDate->setTimezone(new DateTimeZone('UTC'));
$localDate->setDate($date[0], $date[1], $date[2]);
if (count($time) == 3) {
$localDate->setTime($time[0], $time[1], $time[2]);
} else {
$localDate->setTime($time[0], $time[1], '00');
}
$localDate->setTimezone(new DateTimeZone('Asia/Kolkata'));
return $localDate->format('d/m/Y H:i:s');
}
I am having problems with dates in php- sometimes the date gets to us in d/m/y and other times its d/m/Y. I want to convert all dates to d/m/Y.
Working with my current dataset, how would I get 24/06/2015 from 24/06/15 using php?
So far I have tried :
$original_date = '24/06/15';
$new_date = date('d/m/Y', strtotime($original_date));
This brings back 01/01/1970
This is probably the most robust method:
$string = '24/06/15';
$date = DateTime::createFromFormat('d/m/y', $string) ?: DateTime::createFromFormat('d/m/Y', $string);
echo $date->format('d/m/Y');
createFromFormat returns false if you try to parse 24/06/2014 using the d/m/y format, so in that case you just retry with d/m/Y. You then get a DateTime object which you can format and output any way you like.
use the lowercase 'y'. See the PHP date manual.
$new_date = date('d/m/y', strtotime($original_date));
y = A two digit representation of a year
The problem is that the strtotime doesn't recognise the UK date format, so convert the format first then format the date.
Try this:
$original_date = "24/06/15";
list($date,$month,$year) = sscanf($original_date, "%d/%d/%d");
$date_convert = $year."-".$month."-".$date;
$new_date = date("d/m/Y", strtotime($date_convert));
echo $new_date;
Its wrong format of date you are using for strtotime.
Have a look at Date Formats
The correct code should have
$original_date = '15/06/24'; // Notice : its mm/dd/yy here
$new_date = date('d/m/Y', strtotime($original_date));
My current date format is
15/May/2015
I want to get it as
05/15/2015
Please help me about this. Thanks in advance.
Given that you have some sort of timestamp already, you can do this:
$date = date('m/d/Y', $timestamp);
If you don't have a timestamp, let's assume you have your current date stored in a variable:
$original = '15/May/2015';
You can then do this:
$date = date('m/d/Y', strtotime($original));
Alternatively, you can use the DateTime class:
$date = DateTime::createFromFormat('d/M/Y', $original)->format('m/d/Y');
// Assumed 'd/M/Y' as current format ^
// Could also be 'j/M/Y', 'd/F/Y', 'j/F/Y', etc.
For more info, check the docs for date, strtotime, and DateTime.
If you want to get the current date in that format, simply use:
$now = date("m/d/Y");
But the docs are your friend: http://php.net/manual/en/function.date.php
I have a timestamp in the database. With the following code I can format it to the right date:
$datefrom=mysql_real_escape_string($record['projectdatefrom']);
$date1 = date("Y/m/d", $datefrom);
Then I give the input vield the value="$date1.
Now I have another field for the H:i, so I'd like to have them seperate from each other.
Can can I cut the Y/m/d of the $date1 and only return the H:i?
Was trying doing things like this: $datetest = date("H:i", $datefrom); but no success.
$datefrom will need to be a UNIX timestamp. strtotime() can be useful for generating one off a plain-text or MySQL-style date string.
There is no need to escape the string after it was returned from the DB.
Therefore:
$date = date("Y/m/d", strtotime($record['projectdatefrom']));
$time = date("H:i", strtotime($record['projectdatefrom']));
Or using DateTime:
$dt = new DateTime($record['projectdatefrom']);
$date = $dt->format('Y/m/d');
$time = $dt->format('H:i');
I have several dates being outputted into variables. They are formatted as follows:
/Date(1341788400000+0100)/
How would I go about formatting them using PHP into:
DD/MM/YYYY HH:MM
Thanks!
I ended up using the following, as the initial format was in milliseconds:
$date = 1341788400000+0100;
$date = ( $date / 1000 );
$date = date("d/m/Y H:m", $date);
$date = 1341788400000+0100;
echo date("Y/m/d H:m",$date);
Unless the +0100 is the actual time of the day (01:00) ?
First, you parse it, e.g. using strtok() http://php.net/manual/en/function.strtok.php
Then parse it as a number.
$seconds = intval($a)
Then format it using
date("Y/m/d H:m", $seconds)`.