I have date string (Example '2014-06-06 12:24:48'
). I what display with other format (Example '06.06.2014 (12:24:48)'
).
$date = '2014-06-06 12:24:48';
echo date('d-m-Y (H:i:s)', strtotime($date));
Does the output date would be always the same as input date? For any valid input date.
date('Y-m-d H:i:s') === date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s')));
// Always true?
date('Y-m-d H:i:s') === date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s')));
// Always true?
While that statement will be true most of the time, it will not always be true because the left (date('Y-m-d H:i:s')
is evaluated after the right date('Y-m-d H:i:s')
, so if the code is run a nanosecond before the second digit is about to change, the two dates will have different second digits.
You can verify that these two statements are not always equal by running the code below, which will eventually print "False".
while(true) {
$x = (date('Y-m-d H:i:s') === date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s'))));
if(!$x) {
exit("False");
}
}
However, if you provide the same timestamp to each date
call, then the statements will always be equal
$time = strtotime(date('Y-m-d H:i:s'));
date('Y-m-d H:i:s',$time) === date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s',$time)));
//Always true
Yes. The two dates will always be the same date and time - in different formats:
2014-06-06 12:24:48
06-06-2014 (12:24:48)
EDIT
The statement:
date('Y-m-d H:i:s') === date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s')));
will always usually be true (See Edit 2 below).
The second parameter on date()
defaults to the current timestamp.
strtotime(date('Y-m-d H:i:s'))
is the current timestamp.
So the two statements:
date('Y-m-d H:i:s')
and
date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s'))
are logically the same.
Edit 2
FuzzyTree's answer points out that, while the two statements are usually true, they will not always be true. See his answer for more details.
Yes, you can change format of date like
$date = '2014-06-06 12:24:48';
echo date('d-m-Y (H:i:s)', strtotime($date));
For more format :- http://www.php.net//manual/en/function.date.php
the output will not be same as your input date it's depend on what you entered date format so if your date format is:-
$date = date('Y-m-d H:i:s');
and input date format is
$input_date = date('Y/m/d H:i:s');
then you need to convert input date format to match with other date like
$input_date = date('Y-m-d H:i:s', strtotime($inputdate));
else your input date format is same then no need to convert
then match if($date == $input_date)