![]() The date() function accepts two arguments: a format string and a timestamp. The output of this would be: It is now 12:20 This date() function is easily one of the most useful functions in this collection - it allows you to massage that long, ugly timestamp into something that's a pleasure to read. Once you've got yourself a timestamp, you can use the date() function to format it and make it look pretty. Here's an example of the former: Īnd here's an example of the latter: When Looks Do Matter The microtime() function is particularly useful when you need to time events accurately, or when you need an arbitrary, unique number to seed PHP's random number generator. Like time(), this function too cannot be used with arbitrary dates and times - it only returns a timestamp for the current instant. These two values can be added together to obtain a more precise timestamp value. If you need greater accuracy in your timestamp, take a look at the microtime() function, which returns a timestamp in seconds, together with an additional microsecond component. If you don't like mktime(), you can also use the time() function, which returns the current timestamp (note that you cannot use this function to generate arbitrary timestamps, as is possible with mktime()). Notice also that the "0" key of the array returned by getdate() contains a UNIX timestamp representation of the date returned. You can obtain a timestamp for the current moment in time by calling mktime() with no arguments: This integer can now be passed to getdate() - or any other date function that accepts timestamps - and converted into a human-readable representation. The result is a long integer like this: 1017651920 In this case, I've passed the mktime() function six parameters: the hour, minute, second, month, day and year for which I need a timestamp. In PHP, UNIX timestamps are created via the mktime() function, which accepts a series of date and time parameters, and converts them into a timestamp. This timestamp is a unique numeric representation of a particular date, calculated as the number of seconds between Januand the date and time specified, and makes it easier to perform arbitrary calculations on date and time values. Most of PHP's date functions work on the basis of timestamps. How did I create the timestamp? Flip the page to find out. ![]() Consider the following example, which sends getdate() a UNIX timestamp for the date December 25 2001, and gets the information back as a much easier-to-read array: You can also give getdate() a timestamp, and have it convert that timestamp into an array for you. This information can easily be manipulated and massaged into whatever format you like. Here's what the output would look like: Array This is best accomplished via the getdate() function, which returns an associative array containing date and time information. The simplest and most basic thing you'll want to do with PHP's date API is, obviously, get the current date and time. Getting A Dateįirst up, before we get into anything too complicated, let's take a quick tour of the important date and time manipulation functions that come with PHP. And, over the course of this article, I'll be looking at how they work, and how they can be used to add something new to your bag of PHP tricks. These capabilities may be implemented as functions, objects or object methods, but they do exist, in more or less similar form, in almost every language. ![]() What does this have to do with anything? Well, almost every language - including PHP, which is the subject of this article - comes with certain basic capabilities for date and time value manipulation. This switch is not necessarily instant - the devil is still in the details - but a sound understanding of basic programming principles can go a long way to make it more painless. This commonality makes it possible for experienced developers to easily switch from one development environment to another. For example, though they may be called by different names, almost every language comes with string, numeric and array variables, conditional tests, loops, functions, exception handlers et al. No matter what your poison may be - Perl, PHP, Python, JSP - every language comes with certain fundamental constructs and concepts that are widely-understood and used. And nowhere is this more true than in the area of programming languages. Someone once famously said, "the more things change, the more they remain the same". ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |