how can i work with dates before 01.01.1970?
i need to get first day of the month for dates from year 1700.
how can i achieve that?
thank you in advance!
You can use DateTime.
Example:
<?php
$d = new DateTime("1780-06-01");
echo $d->format("l"); //Thursday
You should, however, consider that the Gregorian Calendar was adopted in different instants throughout the world.
Try using the Zend Date classes, or the older PEAR Date class
If the year is <1970 or the value is negative, the relationship is undefined.
-- The Open Group, Single Unix Specification, Base definitions (4: General concepts)
As you can see, it's not defined how Unix timestamps before 1970 would map to dates. It is only defined for later dates.
If you want to go to dates before 1970, you need to always work with actual dates, like ISO dates (2008-12-31T23:59:59). You can use DateTime class to represent any date you can think of but ever created it from a Unix timestamp or convert it to a Unix timestamp if the year is before 1970 as that will simply yield undefined results.
Related
In Laravel, the Illuminate\Database\Schema\Blueprint class has two methods that I would like to know concretely the difference between them.
$table->dateTime()
and
$table->timestamp()
Both store a date in the same way visibly.
Someone to enlighten me?
So the secret to this is understanding what exactly each does.
The dateTime() and timestamp() functions here in Laravel use different table columns.
dateTime() uses DATETIME as the DB column type.
timestamp() uses TIMESTAMP as the DB column type.
DATETIME and TIMESTAMP have a lot of similarities but the difference itself lies outside Laravel and more in MySQL.
Their major difference is the range. For DateTime its up to year 9999 while for timestamp its only up to year 2038. Other differences include the amount of bytes needed to store each.
I found a nice article that clearly states the similarities and differences of both here http://www.c-sharpcorner.com/article/difference-between-mysql-datetime-and-timestamp-datatypes/
Hope this helps.
$table->dateTime()
Create a new date-time column on the table. While on the other hand,
$table->timestamp()
Create a new timestamp column on the table.
If you have a problem identifying the difference between timestamp and datetime,
DATETIME represents a date (as found in a calendar) and a time (as can be observed on a wall clock)
And,
TIMESTAMP represents a well defined point in time. This could be very important if your application handles time zones. How long ago was '2010-09-01 16:31:00'? It depends on what timezone you're in.
Also, you can refer to BluePrint Documentation for any inconveniences.
timestamp and dateTime store a date (YYYY-MM-DD) and time (HH:MM:SS) together in a single field i.e. YYYY-MM-DD HH:MM:SS.
The difference between the two is that timestamp can use CURRENT_TIMESTAMP as its value, whenever the database record is updated.
timestamp has a limit of 1970-01-01 00:00:01 UTC to 2038-01-19 03:14:07 UTC dateTime has a range of 1000-01-01 00:00:00 to 9999-12-31 23:59:59 UTC
timestamps doesn't take an argument, it's a shortcut to add the created_at and updated_at timestamp fields to your database.
I have two date:
2014-12-01T12:05:59Z //dont know which format
And
2014-12-01T03:59:00-08:00 //i'll get it from date("c") function in php (ISO 8601).
So, I don't know what is the different in this date's?
And how to convert date in 2014-12-01T12:05:59Z this formate in php?
[UPDATE]
I want to get current timestamps in in 2014-12-01T12:05:59Z this date formate.
It's the ISO 8601 format and both time zone variants are allowed and must be expected. -08:00 means 8 hours behind UTC, Z means UTC and is a shortcut for +00:00.
Both the dates you have mentioned are dates with timezone. The only difference is that the first date has 'z' at the end which is a Zone Designator for UTC and the later date is suffixed with timezone offset, which is generally represented as "HH:MM". Practically, both dates are same and both the representations are correct.
The 'z' suffixed representation is generally accepted xml standard for timestamp in API payloads.
Looking at your requirements, since your API provider needs the date with Zone Designator, they must be calculating time in UTC.
I will suggest changing your timezone to UTC first using
date_default_timezone_set('UTC');
and then use this expression to get required timestamp
date('Y-m-d\TH:i:s\Z', time());
This way you are saved from the timezone conflict and you will also be able to send the required date to the API.
date_default_timezone_set('UTC');
echo gmdate('Y-m-d\TH:i:s\Z');
Check the link. You will get the difference as well.
Hi i am working on facebook Graph API where i need all the posts information of a group. So I did it and saw [created_date'] => '2013-01-25T00:11:02+0000' what does this date and time represent i mean i know 2013-01-25 is date and 00:11:02 is time but what does T and +0000 represent.
BTW where is the server of facebook. Which timestamp should i use to match facebook time?
Thank you.
T = TIME and the +0000 is timezone offset. Facebook uses localized timezones. You can request a Unix timestamp instead of the string by adding the parameter: date_format=U to your graph API call.
Please see this link for more information.
The date format is called ISO 8601. The letter T is used to separate date and time unambiguously and +0000 is used to signify the timezone offset, in this case GMT or UTC.
That said, you generally don't need to worry so much about the actual contents; rather you should know how to work with them. To use such a date, you can use strtotime() to convert it into a time-stamp:
$ts = strtotime('2013-01-25T00:11:02+0000');
To convert the time-stamp back into a string representation, you can simply use gmdate() with the predefined date constant DATE_ISO8601:
echo gmdate(DATE_ISO8601, $ts);
Alternatively, using DateTime:
// import date
$d = DateTime::createFromFormat(DateTime::ISO8601, '2013-01-25T00:11:02+0000');
// export date
echo $dd->format(DateTime::ISO8601), PHP_EOL;
This is a standard format, specifically ISO 8601.
As much as I don't like linking to it, http://www.w3schools.com/schema/schema_dtypes_date.asp does have a good "human-understandable" explanation:
The dateTime is specified in the following form "YYYY-MM-DDThh:mm:ss"
where:
YYYY indicates the year
MM indicates the month
DD indicates the day
T indicates the start of the required time section
hh indicates the hour
mm indicates the minute
ss indicates the second
To specify a time zone, you can either enter a dateTime in UTC time by
adding a "Z" behind the time - like this:
2002-05-30T09:30:10Z
or you can specify an offset from the UTC time by adding a positive or
negative time behind the time - like this:
2002-05-30T09:30:10-06:00
or
2002-05-30T09:30:10+06:00
Therefore, in your case the +0000 indicates a time offset of 0 from UTC.
This question already has answers here:
Using strtotime for dates before 1970
(7 answers)
Closed 7 years ago.
Hello guys is there a way to convert the date 0001-01-1 to a time format? I am trying to to use the php function strtotime("0001-01-01"), but it returns false.
My goal is to count the days from the date: 0001-01-01 to: 2011-01-01.
Use DateTime class. strtotime returns a timestamp which in your case will be out of int bounds.
As Ignacio Vazquez-Abrams has commented, dates before the adoption of the Gregorian calendar are going to be problematic:
The Gregorian calendar was adopted at different times in different countries (anywhere from 1582 to 1929). According to Wikipedia, a few locales still use the Julian calendar.
Days needed to be "removed" to switch to the Gregorian calendar, and the exact "missing" dates differ between countries (e.g. the missing days in September 1752). Some countries tried to do a gradual change (by removing February 29 for a few years); Sweden switched back to the Julian calendar to "avoid confusion", resulting in a year with February 30.
Years were not always counted from January 1 (which has left its legacy in things like the UK tax year).
Michael Portwood's post The Amazing Disappearing Days gives a reasonable summary.
In short, you have to know precisely what you mean by "1 Jan 1 AD" for your question to make any sense. Astronomers use the Julian Day Number (not entirely related to the Julian calendar) to avoid this problem.
EDIT: You even have to be careful when things claim to use the "proleptic Gregorian calendar". This is supposed to be the Gregorian calendar extended backwards, but on Symbian, years before 1600 observe the old leap year rule (i.e. Symbian's "year 1200" is not a leap year, where in the proleptic Gregorian calendar it is).
I am afraid it wont work since strtotime changes string to timestamp, which have lowest value of 0, and its at 1970-01-01.. cant go lower than that..
date('Y-m-d', 0);
You cannot do that this way. Here is one option how to do this.
$date1= "2011-01-01";
$date2 = "2011-10-03";
//calculate days between dates
$time_difference = strtotime($date2) - strtotime($date1);
now you got time difference in seconds from wich you can get days, hours, minutes, seconds.
From the manual:
If the number of the year is specified in a two digit format, the values between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999. See the notes below for possible differences on 32bit systems (possible dates might end on 2038-01-19 03:14:07).
You may want to give 'DateTime::createFromFormat' a shot instead this.
Also note that you cannot go below 1901 on a 32-bit version of PHP.
You can't use the function strtotime because this function return a timestamp and the time stamp start from 1970 so i advise you to searsh for a different way
I am not sure why strtotime() in PHP returns different result in different timezone even though same date is given as parameter, does anyone know the answer? I also want to know, can I do similar task (converting a datetime to an int to do calculations easily) with another function which gives same result across different timezone?
EDIT:
An example:
If I use strtotime('2011-09-19 00:00:00') shouldn't it just return the difference between 'January 1 1970 00:00:00' and '2011-09-19 00:00:00' in seconds ? Why timezone is an issue here? And can I get something which gives just difference without timezone issue?
In short: time zone is considered because the Unix Epoch value is considered in GMT.
In broader sense 2011-09-19 00:00:00 comes to Bangladesh almost after 6 hours it is 2011-09-19 00:00:00 in GMT zone. Because of this gap, another 21600 seconds have passed in the GMT zone when the same date appears in BD.
Since the calculation is done in respect to the GMT, you have to add these 21600 seconds to get the actual difference.
strtotime gives different results in different timezones because it takes timezones into account...
From strtotime's manual:
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC)
This function will use the TZ environment variable (if available) to calculate the timestamp. Since PHP 5.1.0 there are easier ways to define the timezone that is used across all date/time functions. That process is explained in the date_default_timezone_get() function page.
Have a look at mktime().
Since PHP 5.1, you can use date_default_timezone_set before calling mktime or strtotime.
From the PHP manual:
This function will use the TZ environment variable (if available) to calculate the timestamp. Since PHP 5.1.0 there are easier ways to define the timezone that is used across all date/time functions. That process is explained in the date_default_timezone_get() function page.
http://php.net/manual/en/function.strtotime.php
Use date_default_timezone_set before calling date/time functions to choose which time zone you want to work in.
http://www.php.net/manual/en/function.date-default-timezone-set.php
From PHP docs on strtotime:
This function will use the TZ environment variable (if available) to
calculate the timestamp. Since PHP 5.1.0 there are easier ways to
define the timezone that is used across all date/time functions. That
process is explained in the date_default_timezone_get() function page.
Try setting your own time zone.
i think probably there will be one time of each php programmer that this function will make him wants to really understand how actually php works with date and time functions.
funny this function when you try something like...err...for example, assume that today at this very moment is July 11th, 2012 at 13:00:00 (2012-07-11 13:00:00) and then you try strtotime to find exactly the same moment of the day but for tomorrow:
$x = strtotime('2012-07-12 13:00:00');
$y = strtotime('+1 Day');
$z = $x-$y;
$x and $y of the above first 2 lines will not return the same thing even you ignore the minute and second counts but $z will be around 25200 or around 7 hours in the difference between this 2 lines if your sever is in somewhere of the USA that the GMT is -5 hours but you browser calls this function from berlin in summer where the GMT is +2 hours... LOL now you can get the idea how php work with this function ;)