I wanna know how many days the calendar started started. So I do this:
$beginning = Carbon::create(0, 1, 1)->startOfDay(); // is: 0000-01-01 00:00:00
$date = Carbon::now()->->startOfDay(); // is: 2016-05-25
$beginning->diffInDays($date)); // return: 736473
That correct, right?
I wanna make it possible to do the same in reverse. What is the date when the day is 800000? Is there somethign already for this in Carbon what I overlooked?
0000-01-01 is not a real date: there was no year 0! The year before 1 AD was 1 BC.
Generally speaking, using a date before 1582 AD as an epoch (i.e, a starting reference point) is inadvisable. The Gregorian calendar was established in 1582 AD -- it can be extended backwards to form the proleptic Gregorian calendar, but some software may handle this inconsistently.
That all being said, you should be able to use the Carbon->addDays() method to do what you're trying to accomplish here:
$date = $beginning->addDays(800000);
Related
Lets say I have a date and time in the following format:
09:24:24 Mar 07, 2014 PST
Using PHP I want to be able to work out what the date and time will be exactly 3 days from that date. Then use that info to display a count down, for example: Time up in 2 days, 3 hours, 27 mins and 5 secs.
And finally display a message when the time is up.
I have no idea how to approach this, can anyone point my in the right direction?
Thank you.
Well there's certainly plenty of ways to approach this, and you haven't posted any code of anything that you've already tried, so the best I can do is point you in the right direction.
I'm assuming you're getting this date from the client (09:24:24 Mar 07, 2014 PST) as a string passed to your PHP script:
The first thing you need to do is parse the string so you can pull out the year, month, day, hour, minutes and seconds. So that you can create a DateTime object in PHP with:
$date = new DateTime();
$date->setDate($year, $month, $day);
$date->setTime($hour, $minute, $second);
Then you can use add() to add 3 days like so:
$date->add(new DateInterval("P3D"));
Now you have your date that you can pass back to your client (javascript) to display some sort of a countdown timer (search google for this, you'll get a plethora of results).
$echo $date->format("Y-m-d H:i:s");
I am well versed in using strtotime or the date modify functions within PHP to find NEXT or PREVIOUS days/months/years, or to add or subtract dates. There is the ability to do something like strtotime("first day of last month") but there is not the ability to use a specific month name (let's say July) as a parameter...so it seems I cannot say strtotime("first day of last July").
My question is NOT
"can you write me some code to give me the date"
but rather
"is there a streamlined 1 or 2 line approach using strtotime() or
something else that will enable me to reach the same output with
something more compact, tidy, and clear?"
I am trying to streamline some fairly clunky code to figure out the date (really just the year) of July 1 two instances ago...not the most recent past July 1, but the one a year prior to that (so it could be a date almost 2 full years in the past...or could be as recent as 1 year and 1 day in the past).
For example:
Assume today is February 26, 2014. I am trying to output 2012-07-01.
However a bit later this same year... let's say on July 2 of 2014...
the output would now be 2013-07-01
So, essentially I need to figure out if July 1 has already happened in the current year...and if not then subtract 2 from the current year...and if yes, then subtract 1 from the current year.
A clunky version looks like this:
$CurrentDate = date("Y-m-d");
$CurrentYear = date("Y");
$ThisYearCutOff = $CurrentYear.'-07-01';
if($CurrentDate > $ThisYearCutOff){
echo ($CurrentYear-1).'-07-01';
}
else
{
echo ($CurrentYear-2).'-07-01';
}
Any thoughts on how to do this in a relatively streamlined bit of php code?
I saw in PHP's documentation that there are two ways to format the year value in 4 digits:
Y - A full numeric representation of a year, 4 digits
o - ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)
I researched more what the ISO-8601 is and it seems to be more "useful" or even accurate(?) based on php's documentation. I think it's more useful because it prevents ambiguity (like when handling date values like 1981-04-05).
But does it really make a difference? Is there some sort of "best practice"? Is 'o' slower that's why people use Y instead when they're not really doing anything complicated with the dates?
What specific use-cases perhaps is this useful?
Thank you for your help.
EDIT:
Thank you Jason for an example! I guess why I brought this up as well is I'm wondering why not just use 'o' all the time? I'm thinking I should just use 'o' all the time for currently unknown future specification that will require me to do more complex date operations.
o would be used when trying to determine the year of the current week (i.e. calendar week).
Y would be used for output of the year for a specific date.
This is shown by the following code snippet:
date('Y v. o', strtotime('2012-12-31')); // output: 2012 v. 2013
Not saying it makes it best-practice, but Y is more commonly used to output year. As noted, o would be incorrect/confusing to output the year for such boundary dates.
ISO 8601 is a very useful standard for representing dates, but it has several components, not all of which are useful for many purposes. In particular, the "ISO week" and "ISO year" can be slightly confusing terms, and they're often not what you really want to use (and if you do want to use them, you probably already know that).
Wikipedia has a pretty good explanation. Basically, the ISO 8601 standard defines a separate calendar that, instead of breaking up into months and days of the month, breaks up into weeks and days of the week.
You might think this wouldn't make a difference for specifying the year, but there's a little wrinkle: which year do the weeks near the beginning and end of the calendar year fit into? As Wikipedia says, it's not quite intuitive: "The first week of a [ISO] year is the week that contains the first Thursday of the [calendar] year."
The practical upshot: If you're representing dates as conventional year-month-day values, you don't want to use the "ISO year" (o). You won't notice the difference for most of the year, but as you get to December and January, suddenly you'll (sometimes) end up with dates that are a year off. If you just test your system with current dates, you can easily miss this.
Instead, you probably just want to be using plain old Y-m-d. Even though the date() documentation doesn't specifically say it, that's a perfectly valid ISO 8601 standard date.
Really, you should only be using o if you're using the ISO "week date" calendar (meaning you're likely also using W for the week number and N for the day-of-week).
I am trying to make a webpage and have the next three events on a Google Calendar show up on the home page. I have been using this PHP (http://james.cridland.net/code/google-calendar.html) to access my XML feed and format it into HTML.
The problem I'm having is that for some reason a new day starts at 11am. For example if my Google Calendar has an event from 10am on the 20th of December that lasts an hour, my PHP output will show an event that starts at 10am on the 20th which ends at 11am on the 21st. Otherwise it is working fine.
I have set my time to local (New Zealand) time on my Google Calendar account, and in PHP using date_default_timezone_set("Pacific/Auckland");
The horrible line that calculates the finish date is
$gCalDateEnd = date($dateformat, strtotime($ns_gd->when->attributes()->endTime)+date("Z",strtotime($ns_gd->when->attributes()->endTime)));
where $dateformat is a string with the date format.
The Google Calendar XML gives a start and finish time of
2011-12-22T10:00:00.000+13:00
2011-12-23T11:00:00.000+13:00
respectively, and the PHP is calculating a timeframe of 10.00am 22 December 2011 to 2.00pm 23 December 2011.
Whats going on?!?!
This line is indeed horrible:
$gCalDateEnd = date($dateformat, strtotime($ns_gd->when->attributes()->endTime)+
date("Z",strtotime($ns_gd->when->attributes()->endTime)));
strtotime can handle this type of ISO 8601 dates just fine. This code-fragment is probably written under the assumption that strtotime dismisses the timezone and returns the datetime in UTC and therefore the timezone "correction" needs to be calculated manually - that's what the +date("Z", ...) stands for (with "Z" the second parameter - the timestamp - is actually ignored).
So in your example 13 hours are added to your dates. And 10:00 + 13:00 = 23:00 (11 pm) which is still on the same day, but 11:00 + 13:00 = 24:00 (12 am) which is actually 00:00 on a new day.
So the correct way to convert the date is:
$gCalDateEnd = date($dateformat, strtotime($ns_gd->when->attributes()->endTime));
Try the zend framework for google calendar(It worked for me better than reinventing the wheel): http://framework.zend.com/manual/en/zend.gdata.calendar.html (look at the examples, they're quite easy and helpful)
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