Tip:
Highlight text to annotate it
X
One of the areas that perplexes a lot of beginning developers is working with
dates and times. That's partly because there are a lot of different ways to do
it and there are a lot of restrictions that depend on what kind of operating
system you have, what kind of database you are using, and things like that that
just make it into a quagmire that's hard to wade through.
So I'm going to give you some of the basics, some of the essential concepts
behind it, and then you will have to take a look at your system and see what
works for you and do some research on ways to work around it, but this will at
least give you the fundamentals. The first thing that we need to talk about
is going to be Epoch time or the Unix timestamp. These are the same things,
and if time sets the computer epoch_began. So basically that's the time
that Unix is going to use for its timestamp. Essentially it's the number of
seconds that have elapsed since January 1, 1970 at midnight. That is the beginning
of the computer epoch according to the Unix timestamp.
Since Unix is a file system, it must have seem like common sense to the people
developing it that a file could not be created before January 1, 1970. The
problem comes in when we start using these dates for things besides just files
that are being created on the computer and start using them to keep track of
birthdays and other calculations, then we might have dates that go into the past.
Now, if we have a 32 bit system, we're able to use dates from December 1902 to
January 2037. 1970+67 years to get to January 2037, or 67 years in the past to
get to December 1902. We do that by having a negative number. So we can count
time forwards and backwards in negative numbers. But that also limits us. We still can't talk
about some birthdays. We can't talk about dates that happened in
history. We can't talk about future, social security payments, and things like that that
might happen past 2037. So that's still a pretty limited range.
So ultimately, what I think is going to happen is most systems will end up
going to a 64 bit system of counting, because that will last for billions of
years and that will take care of all of our needs, but we haven't done that yet.
So on your system, you may be limited to 1902-2037, and if you are on a Windows
machine or maybe some of the older Linux distributions, even dates prior to
1970 might give you a problem. I think I ran into that with the IIS database.
It had problems with dates before 1970, and that's because Windows does.
If you ran into one of those problems, what are you going to do? Well, there
are many choices and it depends on your needs. You can store the date as a
string, you can store the year separately from the date, or you could read up
on one of the many hacks that are out there on ways to get around it. The
php.net website is full of a lot of those. But for the purposes of this tutorial I don't
want to focus on how you are going to work around the limitations of the
computer you are working with, because you may get a shiny new compute tomorrow
that handles all of it fine, instead I want to talk about the fundamental
principles that we're going to be working with to create a Unix timestamp.
There are three ways that I want to look at to do that. The first is just time.
time with no arguments, just have the parenthesis and that will
return the current time. Remember that's the number of seconds since 1970.
So that's the Unix timestamp. Now, the second is that we can make time,
mktime and we just need to provide it the number of hours, minutes, seconds,
and then month, day, and year. It will come back to us with a Unix timestamp,
the number of seconds since 1970. Now, notice the order there, it gets a little
confusing for some people. It has the time first, hour, minute, second, and
then it has month, day, year, which is sort of the American way, instead of day,
month, year, like they do in other countries. So they are not in sort of ascending
order or anything like that, it's a little bit of a jumble, but that's
the order it's going to be. An hour, minute, second, month, day, year.
The last is string to time, strtotime, and then we provide any string to that
function and it will return a Unix timestamp to us, doing the best it can to
generate that timestamp based on whatever is in that string. We'll take a look
at that, it's a pretty neat little function. So I'm going to open back up array_ functions,
and I'm just going to do a Save As on that file, and I'm going to call this
date_time_unix.php, just to remember, because we're going to take a look
at some other date, time features later. I'm going to take out all of this and I'm
going to call it Dates and Times: Unix.
Now, the first way that we're going to make Unix time we said was just with the
simple Time command. So we'll do that with just echo time;. We'll save that,
and let's open that up in our browser and see what we get back. So we'll just
open up a new window and we'll go back to our sandbox/date_time_unix.php.
There we go. That's how many seconds have elapsed. If we reload it, we can see all
the seconds as they continue to count. Now, we'll learn how to format that into something
that looks nicer a little later. What we're interested right now is
just getting that Unix timestamp, because once we have a Unix timestamp, we
can start to compare different timestamps. We can see when something happened
5 minutes ago, 10 minutes ago, by comparing the current timestamp with the
timestamp that we have stored, let's say, in a database or in a file or something
like that. The second way we saw that we could make a
timestamp is with mktime, and you will remember that I told you that we
have to provide the number of arguments here. We can do those as variables or just simply
integers. So the first of those is going to be the hour, let's say 2,
and the minute 30, and the seconds we'll make 45. Then we need to put in the
month and the date and the year, and I'll put in 2009. Semicolon there and let's,
just for good measure, let's put in tag at the end of this just so it
will be on a new line. Let's reload it. There we go.
So now you see the difference between the current time and the number of
seconds there, and this date in the future for 2:30:45 on October 1, 2009.
We can actually subtract the two and find out how many seconds there are in
between and then convert that to a number of days and so on.
Now, one note is that if you give something invalid to mktime, it will return false.
So if you try to tell it the date was February 30, that's an invalid
date and it would return false. Now, there is also another way that we can
check whether dates are true that's worth mentioning, and that is with checkdate.
If we say, for example, echo checkdate (12, 31, 2000), and then we'll ask it to
return 'true', if that's a true date, or 'false', if it's not. We can do the
same thing again. Let's go ahead and put our brs in here, just to make it nice and clean,
but this time let's ask it for February 31. Let's see whether that's a date.
So if we come back and we reload those pages, you will see that the first one
is a date and the second one is not a date. So checkdate is also a helpful way
to check whether a date is there or not. Last of all, I want us to take a look at that
really cool function, the strtotime. So let's say $unix_ timestamp = strtotime,
and then we can put in a string that designates whatever time we
would like. Now, this is the cool part. For example, let's try "now". Then I'm
going to need to echo back that $unix_timestamp if I set that as a value.
I'll append a br after it. Let's load that up and see what we have got.
So there we are. Notice the times are the same at the top and the bottom. time("now"),
there is no difference whatsoever. I can also put in something like
"15 September 2004", and then come back. There is the date stamp for that. I
could also do "September 15 2004", and you will see I get the same date stamp.
So it's able to interpret these different ones.
Let's try "+1 day". That's tomorrow, or we can say something like "last Monday".
So there are a lot of these different things that we can do, that
we can pass in as a string and it's able to interpret what we mean. Now, it isn't
always 100% successful, you will have to play around and try it out a little bit,
but it does a pretty good job of parsing it out and figuring out what you mean and
turning that into a Unix timestamp. So keep that in your back pocket
because that's a really useful tool. Now that we know how to make Unix timestamps,
in the next movie we'll talk about how to make those into something that's
human readable, how to go from a timestamp back to something that we can display,
that looks nice as output on our screen.