Well, I have this code here
$rowValue[$y] = '16/08/2013';
$replaceThis = array("/");
$rowValue[$y] = str_replace($replaceThis, "-", $rowValue[$y]);
this will produce an output with string datatype, '16-08-2013'
and when I try to do this
$rowValue[$y] = DateTime::createFromFormat('d-M-Y H:i:s e', $rowValue[$y]);
echo $rowValue[$y];
it doesn't work, or simply it just displays blank...
You're doing it incorrectly you need to give your current/passed date format to createFromFormat method (i.e Y-m-d in your case) and than after creating DateTime object you can format it using format method.
$rowValue[$y] = DateTime::createFromFormat('d-m-Y', $rowValue[$y]);
echo $rowValue[$y]->format('d-M-Y H:i:s e');
DEMO.
Check the DateTime format you're using. From the docs:
http://www.php.net//manual/en/datetime.createfromformat.php
'M' is used for "a textual representation of a month, such as January or Sept." You need to use 'm' or 'n' instead, which is used for a "numeric representation of a month, with or without leading zeros." Since your string representation has a leading 0, you'll want 'm'.
Your code should look like the following:
$rowValue[$y] = DateTime::createFromFormat('d-m-Y H:i:s e', $rowValue[$y]);
I don't understand why ''d-M-Y H:i:s e' doesn't work.. but thanks, it's now working. Still 3 mins til I accept your answer tho
it does not work because your string does not have time. You must supply the current format of your string which is "d-m-Y" in DateTime::createFromFormat().
Related
I made a function in php to convert date and time coming from a txt to the mysql standard.
But she is turning the month wrong.
I have tried all these conversions but to no avail.
I would like your help because I don't know what else to do.
function convertstringdate('05/02/202116:43:49'){
$date = new DateTime($datetime);
return date_format($date, "Y-m-d H:i:s");
}
or
$input = '05/02/202116:43:49';
$date = strtotime($input);
echo date('Y-m-d H:i:s', $date);
Since you know the format of your datetime-string I suggest you use createFromFormat() like so:
$string = '05/02/202116:43:49';
$dateTime = DateTime::createFromFormat('d/m/YG:i:s', $string);
var_dump(date_format($dateTime, "Y-m-d H:i:s"));
// output: string(19) "2021-02-05 16:43:49"
Note that I am not sure if 05 or 02 is supposed to be the month in your example, so if this seems wrong to you you might just have to switch around d/m in the format string and make it m/d.
For explanation what character means what poriton of the datetime, see the linked above documentation reference.
I am dealing with a problem of time conversion from 12 hr format to 24 hour format.
Is there any single function in php to replace the first two characters of a string?
str_replace can be used only when I know the substring content to be replaced.
$str_to_replace = '12';
$input_str = 'ab345678';
$output_str = $str_to_replace . substr($input_str, 2);
echo $output_str;
"12345678"
If the date is always given in a specific format you could try to convert it to a DateTime object and format the output.
$dateString = '15-Feb-2009 2:24 PM';
$date = DateTime::createFromFormat('j-M-Y g:i A', $dateString);
echo $date->format('Y-m-d G:i'); // will show "2009-02-15 14:24"
In general you should try in avoid holding a date in a string. Convert it to a DateTime -- this makes it also easier for you to manipulate the object (e.g. move date +1 day)
I have much data with several timestamps and I just recognized that some are in "dd.mm.YYYY" which works very well with date("Y-m-d", strtotime($input)); but some are in "dd.mm.YY" and this does not work anymore - it always returns the current date.
My problem is that my data is too huge to fix this problem manually by editting. Is there any way to get the YYYY-mm-dd out of mm.dd.YY ?
Here you go...
$date = "20.02.71"; // sample date... (common German format)
$date = DateTime::createFromFormat('d.m.y', $date);
echo $date->format('Y-m-d');
will result in:
1971-02-20
Create a DateTime object, then format it to anything you want...
Well you can replace the . by -, you could do something like the following:
$date = str_replace(".", "-", "mm.dd.YY")
This would return
mm-dd-YY
You could use date_parse_from_format which would convert any formate into the formate you specify.
date_parse_from_format("y-m-d", $date);
It returns an array with very useful information like month, year etc.
I have this string
$chktodate = "15/02/2014 9 am";
And when I am doing this
$timetocheck = strtotime($chkdt);
It gives me nothing.
Is it not that the function is supposed to take the string like the format which I mentioned ie 15/02/2014 9 am.
strtotime() doesn't understand that format, and hence returns FALSE. The list of formats recognized by the parser are listed here.
Use DateTime::createFromFormat() to parse this format:
$dateObj = DateTime::createFromFormat('d/m/Y g a', $chktodate);
echo $dateObj->format('Y-m-d H:i:s'); // 2014-02-15 09:00:00
For a list of all the available formatting options, see the documentation.
An iCalender file expects the DTSTART and DTEND parameters in its file to be of the format:
20140715T035959Z
Basically, long form year, double digit month, double digit day, the letter 'T' to break the date from the time, then double digit hour, minute, second, etc. appended with the letter 'Z'.
I currently have a date in the following PHP format:
Y-m-d H:i:s
I'm currently trying to format it with the DateTime::format method into an iCalender accepted string, and I thought this might work:
format('Ymd\THis\Z');
I've escaped the characters T and Z in the hopes they would appear, but when my event is echoed into the file, it's simply empty. I have a feeling my representation of the iCal datetime format is incorrect. Ideas?
Current iCal code:
DTSTART:".$calData->eventStart()."
Current $calData->eventStart() code:
public function eventStart() {
$inputDateTime = $this->details['date_time'];
// Convert MySQL datetime to ical datetime
$temp = DateTime::createFromFormat('Y-m-d H:i:s', $inputDateTime);
$eventStart = $temp->format('Ymd\THis\Z');
echo $eventStart; // This should be RETURN, not ECHO!
}
ANSWER:
Yeah, so it turns out this was a non-question. I was simply echoing the datetime instead of returning it.
You could try something like this...
<?php
$pubDt='20140715T035959Z';
$pubDt=str_replace(array('T','Z'),array('',''),$pubDt);
$format = 'Ymdhis';
$date = DateTime::createFromFormat($format, $pubDt);
echo $newPubdate = $date->format('Y-m-d H:i:s'); //"prints" 2014-07-15 03:59:59