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.
Related
This question already has answers here:
How to get time difference in minutes in PHP
(21 answers)
Closed 5 years ago.
I have this php code
$today_date = date('d/m/Y H:i:s');
$Expierdate = '09/06/2017 21:45:03';
$remaindate = date_diff($today_date,$Expierdate);
echo $remaindate;
and i need result from difference between two date.
date_diff() needs a DateTimeInterface as an argument. In other words, you need to create a DateTime object first, using new DateTime() as shown below.
$today_date = new DateTime();
$Expierdate = new DateTime('09/06/2017 21:45:03');
$remaindate = $today_date->diff($Expierdate);
echo $remaindate->format('%a days');
Live demo
The above would output
90 days
Because today is June 8th, and the format 09/06/2017 is September 6th - because you're using American format (MM/DD/YYYY).
If you ment June 9th (tomorrow), you need to use European format (MM-DD-YYYY, note the dash instead of slash). You can alternatively use DateTime::createFromFormat() to create from a set format, so your current format, 09/06/2017, is interpreted as June 9th. The code would then be
$today_date = new DateTime();
$Expierdate = DateTime::createFromFormat('d/m/Y H:i:s', '09/06/2017 21:45:03');
$remaindate = $today_date->diff($Expierdate);
echo $remaindate->format('%a days');
Output (live demo)
1 days
In any case, $remaindate holds some properties which can be used (see the manual), or you can format it to your liking by supplying the desired formation into the format() method.
new DateTime()
DateTime::diff()
DateTime::format()
DateTime::create_from_format()
So I have an array of many images in it with their datetimes in the format Y-m-d H:i:s And I wish to find the number of days between the image's date and the current date. This is where I have reached till now...and I'm getting a new error for every small change I make.
$myDateTime = DateTime::createFromFormat('Y-m-d H:i:s', $current_time);
$currentDate = $myDateTime->format('Y-m-d');
foreach($all_images as $key => $am){
$myDateTime1 = DateTime::createFromFormat('Y-m-d H:i:s', $am['datetime']);
$imageDate = $myDateTime1->format('Y-m-d');
$datediff=date_diff($currentDate,$imageDate);
echo $datediff;
}
I'm getting this error:
Warning: date_diff() expects parameter 1 to be DateTimeInterface, string given
Any help would be appreciated! Thanks a lot!
What you've done is you've converted your values to Strings before comparing them, so it's no longer comparing the difference between two dates but instead the difference between two strings. This is your error cause.
Solution:
The values you pass to date_diff need to be two DateTime objects as per the manual:
(PHP 5 >= 5.3.0, PHP 7)
DateTime::diff -- DateTimeImmutable::diff -- DateTimeInterface::diff -- date_diff — Returns the difference between two DateTime objects
Suggestion:
foreach($all_images as $key => $am){
$myDateTime1 = DateTime::createFromFormat('Y-m-d H:i:s', $am['datetime']);
$imageDate = $myDateTime1->format('Y-m-d');
$datediff=date_diff($myDateTime, $myDateTime1);
echo $datediff->format('%R%a days'); // +2 days
}
Note that the above date_diff function takes Objects not strings.
This will now use date_diff [procedurally in this example] to output a difference value $datediff which you can use with DateTime formatting to reach the number of days/hours/whatever. Please Read the manual.
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().
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
I am using Datetime class on PHP.
you can change datetimeclass to string like this.
$date->format('Y-m-d H:i:s')
// it shows 2013-08-05 10:00:00
but somehow ,Google API requires format like this .
2013-08-05T10:00:00
What this T means ?
and How can I make this style string from DateTime class neatly?
The time is in ISO 8601 format. To print it out, you can use 'c' format character:
$date->format('c')
You could use jh314's solution above, and it will give you the time in following format:
2013-08-08T10:18:15+05:30
However, to format it exactly like you want, you could use the following:
$part1 = $date->format('Y-m-d'); // 2013-08-08
$part2 = $date->format('H:i:s'); // 10:19:37
$newdate = "{$part1}T{$part2}"; // 2013-08-08T10:19:37
Or better yet:
$date = $date->format('Y-m-d\TH:i:s'); // 2013-08-08T10:19:37
Ta-dah!
This is ISO 8601 datetime format check this
$date->format('c') //Output 2004-02-12T15:19:21+00:00
This is almost, but not quite ISO8601 format, so you need to format the output like this:-
$date = new \DateTime();
echo $date->format('Y-m-d\TH:i:s');
The \ escapes the 'T'. See the manual about formatting dates.
See it working