Intricacies on DateTime - php

Why is this odd behaviour ?
I want to create this date 2009-02-15 to my specified format, but it shows a year of 1970 . Why is this happening ? I found this example on PHP.net manual.
<?php
$format = 'Y-m-!d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n"; //Output is Format: Y-m-!d H:i:s; 1970-01-15 15:16:17

That's because of the !. From the manual for DateTime::createFromFormat:
If format contains the character !, then portions of the generated time not provided in format, as well as values to the left-hand side of the !, will be set to corresponding values from the Unix epoch.
This means that anything left of the ! will be set to 1970-01-01 00:00:00 UTC. In your case (Y-m-!d H:i:s) that means that the year and month will be set to January 1970, while the remaining parts of the date will be set according to your string.

Why is it ! sign in precise format? It should not be there. Correct format is Y-m-d H:i:s, so:
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
-see manual page, it explains meaning of ! (and you need not have it in precise format)

Check It
$date = date('Y-m-d H:i:s',strtotime('2009-02-15 15:16:17'));
echo $date;

Related

How do I convert this time format - 1480550400000+0000 in Y/m/d date format using php

I am trying to convert this time format - 1480550400000+0000 in Y/m/d date format using php date('Y/m/d',1480550400000+0000); but its not working. How can I make it work?
You timestamp has microseconds, so first remove it.
<?php
$timestamp = 1480550400000+0000;
$timestamp = intval($timestamp/1000);
$date = date("Y/m/d", $timestamp);
echo $date;
output: Check the live demo.
ei#localhost:~$ php test.php
2016/12/01
$dig_date= 1480550400000+0000;
$date = DateTime::createFromFormat('YmdGis', $dig_date);
echo $date->format('Y-m-d G:i:s');
Please note that the second parameter should be time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). Looks like you have entered milliseconds instead.
PHP date()
string date ( string $format [, int $timestamp = time() ] )
timestamp
The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is not given. In other words, it defaults to the value of time().
Try this:
echo date('Y/m/d',1480550400+0000); // 2016/11/30
#Symplifys try this:
<?php
$date = date("Y-m-d", "1480550400000+0000");``
echo $date;
?>
Remember to put timestamp in double quotes.
Try this code at http://phpfiddle.org/
Solved I just divided this with 1000. $date = date("Y-m-d", $timestamp/1000); and it worked
Thanks

converting php string with date and time to sql timestamp

I have a php string which contains the date and time as shown below
2015-05-27 18:45:31
How can I convert this to a type which I can insert into postgresql table with timestamp data type?
Convert it using strtotime() function:
$unix_timestamp=strtotime('2015-05-27 18:45:31');
Thes use $unix_timestamp in your SQL query.
Also you can use TIMESTAMP '2015-05-27 18:45:31' notation right in SQL statement.
If you have another format of date, you can use DateTime::createFromFormat
In your case:
$dateString = '2015-05-27 18:45:31';
$format = 'Y-m-d H:i:s';
$dateConvert = DateTime::createFromFormat($format, $dateString);
echo "Date: $format; " . $dateConvert->format('Y-m-d H:i:s') . "\n";
The output would be something like:
Date: Y-m-d H:i:s; 2015-05-27 18:45:31

Issue converting from Y/m/d to Y-m-d using $date = date('Y-m-d H:i:s', $date);

I'm trying to convert the brackets to hyphens, but instead the date variable loses its value:
echo $date; // outputs 26/05/2015 10:41:56sd2
$date = date('Y-m-d H:i:s', $date);
echo $date; // outputs 969-12-31 18:00:26
The second parameter to date() must be a Unix timestamp. You're giving it a string.
That date format is invalid and won't work with strtotime() anyway. When you use / as the date separator US format is assumed. There is no 26th month.
The last three characters of that is not valid in any standard that I know of and will break any date function unless you specifically account for it (which you can't do with date() or strtotime())
Use DateTime::createFromFormat() to do this:
$date = DateTime::createFromFormat('d/m/Y H:i:s???', '26/05/2015 10:41:56sd2');
echo $date->format('Y-m-d H:i:s');
Demo

Why does strtotime gives different values

See the below code
$compDate = date('d/m/y',strtotime('-2 weeks'));
echo strtotime($compDate)."-->".strtotime('-2 weeks');
The echo outputs 1398882600-->1388938336.
Why does the time stamp differ?
This is from the PHP manual:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
So change d/m/y to either d-m-y or m/d/y and strtotime will work perfectly.
Update: Yes, kingkero is right. You have to change d/m/y to either d-m-y H:i:s or m/d/y H:i:s. The point is that you can’t ignore the hour, minute and second.
The first problem is the wrong format - as stated by Sharanya Dutta. The second one is you are losing precision when formatting to m/d/y instead of m/d/y, H:i:s eg.
$compDate = date('m/d/y',strtotime('-2 weeks'));
print date("d.m.Y # H:i:s", strtotime($compDate)); //05.01.2014 # 00:00:00
print date("d.m.Y # H:i:s", strtotime('-2 weeks')); //05.01.2014 # 17:23:42
When you add hours, minutes and seconds to $compDate, it will work as expected
$compDate = date('m/d/y, H:i:s',strtotime('-2 weeks'));
var_dump( strtotime($compDate) === strtotime('-2 weeks')); // TRUE
If you don't need the time but only the date, you can set the time in the strtotime() as well.
$compDate = date('m/d/y',strtotime('-2 weeks'));
var_dump( strtotime($compDate) === strtotime('-2 weeks, 0:0:0')); // TRUE
strtotime assumes x/x/x notation is American and x-x-x notation as European. Since you are passing in d/m/Y as the formatting for $compDate and then passing that date string through strtotime() as second time, it is interpreting the date string as m/d/Y the second time. You can confirm by changing your code like this:
$compDate = strtotime('-2 weeks');
echo $compDate . "-->" . strtotime('-2 weeks');
or this way:
$compDate = date('d-m-y',strtotime('-2 weeks'));
echo strtotime($compDate) . "-->" . strtotime('-2 weeks');
or this way:
$compDate = date('m/d/y',strtotime('-2 weeks'));
echo strtotime($compDate) . "-->" . strtotime('-2 weeks');
A better solution would be to use the DateTime class, which allows you to define the date format of a given string using createFromFormat so that the parser knows which number is which:
$compDate = date('d/m/y',strtotime('-2 weeks'));
$compDateObject = DateTime::createFromFormat('d/m/y', $compDate);
echo $compDateObject->format('U') . "-->" . strtotime('-2 weeks');
That last example is a bit convoluted because generally with a relative string like -2 weeks you wouldn't also need to worry about the formatting, but I'm guessing the issue you're really having is that the date format used throughout your code is in d/m/y, so the above gives an idea of how to swap between that format and and epoch timestamp. If you want to use DateTime to get a relative date (like -2 weeks), you could revamp the above as:
$compDate = new DateTime('-2 weeks');
echo $compDate->format('U') . "-->" . strtotime('-2 weeks');
echo $compDate->format('d/m/Y');
If you want the timestamp to be for the date but don't want to use time, you can add today to the relative format, like so:
$compDate = new DateTime('2 weeks ago today');
echo $compDate->format('U') . "-->" . strtotime('-2 weeks 00:00');
echo $compDate->format('d/m/Y');

Add minutes to current time

I am trying to add minutes to current date but it returns strange results
date_default_timezone_set('Asia/Karachi');
$currentDate = date("m-d-Y H:i:s");
$currentDate_timestamp = strtotime($currentDate);
$endDate_months = strtotime("+10 minutes", $currentDate_timestamp);
$packageEndDate = date("m-d-Y H:i:s", $endDate_months);
echo " <br> " . $packageEndDate . " <br> ";
echo $currentDate;
I am getting Output
01-01-1970 05:50:00
07-19-2013 20:25:23
It should return
07-19-2013 20:35:23
07-19-2013 20:25:23
After this I need to query to database so date format should be same. Database column is of string type.
Your code is redundant. Why format a timestamp as a string, then convert that string back to a timestamp?
Try
$now = time();
$ten_minutes = $now + (10 * 60);
$startDate = date('m-d-Y H:i:s', $now);
$endDate = date('m-d-Y H:i:s', $ten_minutes);
instead.
Probably the minimalist way would be:
date_default_timezone_set('Asia/Baku');
$packageEndDate = date('Y-m-d H:i:s', strtotime('+10 minute'));
echo $packageEndDate;
Output (Current time in my city at the time of writing):
2017-07-20 12:45:17
Try this:
$now = time();
$tenMinFromNow = date("m-d-Y H:i:s", strtotime('+10 minutes', $time));
$tenMinsFromNow = (new \DateTime())->add(new \DateInterval('PT10M'));
Will leave you with a DateTime object representing a time 10 minutes in the future. Which will allow you to do something like:-
echo $tenMinsFromNow->format('d/m/Y H:i:s');
See it working
PHP version >= 5.4 I'm afraid, but you should be using at least that version by now anyway.
Pakistan, which is the localisation explicitly set, uses "DD-MM-YYYY" format dates so the problem occurs when you cast the date into a string of "MM-DD-YYYY". This American format of date is not parseable by the Pakistan localisation.
If you still want to keep the round-trip to a string and back, use DD-MM-YYYY or the ISO datetime format.
While this is the only (current) answer which actually explains your original issue, I recommend the code be refactored as others have demonstrated.

Categories