I am trying to use php datetime object for handling dates.
Here is my code:
$date = new DateTime('01 Dec, 1969');
echo $date->format('Y-m-d');
The above code returns 2010-12-01
But If I change year from 1969 to 1945 or anything less than 1960 then the code returns incorrect year. For example:
This code:
$date = new DateTime('01 Dec, 1950');
echo $date->format('Y-m-d');
returns
2010-12-01
This is likely a bug. Consider filing it to the bugtracker.
When you change the input format to
$date = new DateTime('Dec 1st, 1950');
echo $date->format('Y-m-d');
PHP will correctly make this into
1950-12-01
See http://codepad.org/trFfB6Q1
As of PHP5.3, you can also use DateTime::createFromFormat to create a date. This would work with your original DateTime string then:
$date = DateTime::createFromFormat('d M, Y', '01 Dec, 1950');
echo $date->format('Y-m-d');
See http://codepad.viper-7.com/08kK5M
Given that this problem does not occur on my system (PHP5.3 on a windows machine)
I suggest you update to php 5.3.
There are no drawbacks and this is probably not the only bug you will run into.
I have tested different date formats('1969/12/1','01 Dec, 1969',..) and had no problems at all.
if the problem persist feel free to slap me ;)
PHP's Datetime is based on a unix timestamp which started counting from 1st of january 1970.
You cannot use DateTime to acces a date before that.
Related
I am getting Google Analytics data via API using google-api-php-client
Everything fine except one thing, I can't convert day timestamp into readable value. The day timestamp looks like 20150724, date('M j', $date); always shows Aug 22 even for different timestamps.
How to fix it?
If you don't tell PHP what the number is, it will assume it's a UNIX timestamp. 20150724 is 22nd August 1970...
So just tell it how it's formatted:
<?php
$google_date = '20150724';
$date = DateTime::createFromFormat('Ymd', $google_date);
echo $date->format('M j');
date() expects the $date to be a timestamp which is the number of seconds since the Unix epoch. Try converting $date with strtotime.
I have been looking online for this answer and have come up empty...I am extremely tired so I thought I would give this a go....
I have a variable that has a date from a textbox
$effectiveDate=$_REQUEST['effectiveDate'];
What I am trying to do is take this date and add the current time
date('Y-m-d H:i:s', strtotime($effectiveDate))
When I echo this out I get 1969-12-31 19:00:00
Is this possible? Can someone point me in the right direction?
I found a solution to my problem....
$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate = date("Y-m-d H:i:s", strtotime($currentDate . $currentTime));
echo $currentDate;
This takes a date from variable in one format and takes the date from another variable in another format and puts them together :)
Thanks everyone for their time.....
DateTime::createFromFormat
would also work but only if you have PHP 5.3 or higher...(I think)
The effectiveDate string is not in a format that strtotime recognizes, so strtotime returns false which is interpreted as 0 which causes the date to be displayed as January 1, 1970 at 00:00:00, minus your time zone offset.
The result you see is caused by the entered date not being in a format recognised by strtotime. The most likely case I can think of without knowing the format you used is that you used the US order of putting the month and day the wrong way around - this confuses strtotime, because if it accepts both then it can't distinguish February 3rd and March 2nd, so it has to reject US-formatted dates.
The most reliable format for strtotime is YYYY-MM-DD HH:ii:ss, as it is unambigous.
The date is just a timestamp, it is not object-oriented and i don't like it.
You can use the DateTime object.
The object-oriented best way is:
$effectiveDate=$_REQUEST['effectiveDate'];
// here you must pass the original format to pass your original string to a DateTimeObject
$dateTimeObject = DateTime::createFromFormat('Y-m-d H:i:s', $effectiveDate);
// here you must pass the desired format
echo $dateTimeObject->format('Y-m-d H:i:s');
Hey guys,
how does one calculate the days past since a date like the one Twitter outputs in its API
eg:
Mon Jul 12 00:27:26 +0000 2010
to XXX
Can we do it with strtotime
Thanks guys,
Dex
Compatibility note: works only for PHP >= 5.3.0
Providing that the date format does not change, you can reverse the process (i.e. reverse timestamp -> string (on Twitters servers) to timestamp) using the exact date format. Using the table on the manual page of DateTime::createFromFormat:
<?php
$str = 'Mon Jul 12 00:27:26 +0000 2010';
$oDateTime = DateTime::createFromFormat('D M d H:i:s T Y', $str);
$unix_timestamp = $oDateTime->getTimestamp();
echo $unix_timestamp;
?>
Beware: On my machine, date('d-m-Y H:i:s', $unix_timestamp) differs two hours, the machines timezone is GMT+2.
To calculate the difference between in days between two Unix timestamps, use math (a day has 86400 seconds):
echo ($unix_timestamp1 - $unix_timestamp2) / 86400;
If you've two such dates, you can use DateTime::diff as suggested in the comments by Zerocrates. You've create two DateTime instances using DateTime::createFromFormat and invoke the DateTime::diff with two arguments passed, previously created DateTime instances. The returned DateInterval instance has a d property which contains the difference in days.
The other way would be using the getTimestamp method, doing the maths from the previous example.
References:
http://php.net/manual/en/datetime.createfromformat.php
http://php.net/manual/en/datetime.gettimestamp.php
http://www.php.net/manual/en/datetime.diff.php
http://www.php.net/manual/en/class.dateinterval.php
You can do it like that (where $your_date is the string you mentioned):
$diff = (time() - strtotime($your_date)) / (24*60*60);
In your case, when I did echo $diff; the output was (at the time I posted the answer):
321.85475694444
See more details for strtotime(), time() and date().
Hope this helped you.
You might find sth here:
performing datetime related operations in PHP
or in php manual there is a lot..
http://php.net/manual/en/function.date.php
I need to convert this date:
10.04.2011 19:00
To a date variable that I can use in PHP.
Can someone help me with that? I tried this way:
$dateConverted = date("d.m.Y H:i",strtotime ($date));
But it returns 01.01.1970 00:00
DateTime::createFromFormat() to the rescue!
It looks like your format is d.m.Y H:i.
So, this should work for you:
$dt = DateTime::createFromFormat('d.m.Y H:i', '10.04.2011 19:00');
echo $dt->format('Y-m-d H:i:s');
You should also take a look at the formats that strtotime and DateTime operate on. In particular, the reason that date didn't parse in strtotime is that it only expects dots as delimiters between Y, M and D if the year is only two digits. That's an odd one, don't look at me, it's not my fault.
Here is what I have:
$dateFormat = 'M d, Y';
$dateString = 'January 23, 2010';
What I need is a timestamp of $dateString so I can do this:
$newFormattedDate = date('Y-m-d', $timestamp);
I tried to use strtotime function but it tries to find out the format itself and doesn't work always. In my situation I know both the date string and date format.
How can I set $timestamp to an appropriate value for use with the date function?
EDIT: I need this to work in both Linux and Windows environments.
EDIT: The solution must support PHP version 4 or higher
EDIT: MySQL has a function called STR_TO_DATE which takes a date string and a date format and returns Y-m-d formatted date string. Any equivalent function for php works for me also.
As of PHP5.3 you can use the DateTime API
$dt = date_create_from_format('M d, Y', 'January 23, 2010');
echo date_timestamp_get($dt);
or with OOP notation
$dt = DateTime::createFromFormat('M d, Y', 'January 23, 2010');
echo $dt->getTimestamp();
Note that while DateTime is available in PHP < 5.3, the methods used above are not and while you could simulate ->getTimestamp() with ->format('U'), there is no easy workaround for createFromFormat()
An alternative would be to use Zend_Date from Zend Framework:
$date = new Zend_Date('Feb 31, 2007', 'MM.dd.yyyy');
echo $date->get(Zend_Date::TIMESTAMP);
Use strptime, mktime and strftime:
$ftime = strptime($dateString, $dateFormat);
$timestamp = mktime($ftime['tm_hour'], $ftime['tm_min'], $ftime['tm_sec'], 1,
$ftime['tm_yday'] + 1, $ftime['tm_year'] + 1900);
echo strftime('Y-m-d', $timestamp);
Please note that strptime is not implemented on Windows platforms.
Note that the arguments to mktime are somewhat unusual - strptime returns a day of the year (between 0-365, inclusive) rather than a day of the month and a month, so we set the month parameter to mktime to 1, and add 1 to the day of the year. Also, strptime returns years since 1900 for the year value it returns, so we need to add 1900 to the year before passing it through to mktime.
Also, you might want to check whether $ftime is FALSE before passing it into mktime. strptime returning FALSE denotes that the inputs $dateString is not a valid date format according to $dateFormat.
Given my understanding of the question and what is available in PHP, you need to relax your expectations somewhere or other.
A 'simple' solution (which has already been discounted) would be to force using PHP 5.3 and the tools available in it.
A less simple solution would be to take those additions and port them over to PHP-land (with PHP 4 compatibility, good luck).
Another route of exploration would be to consider the occasions where strtotime does not work for you and working around those limitations.
How widely variant are your format strings? It may be possible to come up with a solution mapping format strings to functions/methods (to do that date parsing) providing they're pretty restricted.
$dateFormat = 'M d, Y'
$timestamp = strtotime($dateString);
echo date($dateFormat, $timestamp);
if i haven't misunderstand your question you can try the code above..