![]() ![]() ![]() php > print date("m/d/y h:i:s a", $unixTime) Of course, you can use other specifiers to define your own custom formats. The “r” formatting string returns the time formatted as specified by RFC 2822. ![]() If the optional timestamp is not provided, the value of time() is used. date() is used to format Unix timestamps into a human readable string, and takes a formatting argument and an optional time argument. Unix time can be easily formatted into just about any string that a human would want to read. It takes an optional Unix timestamp argument, but defaults to the value of time() if one isn’t provided. If you need an array representation of the Unix time, use the getdate() function. To illustrate this, I will use the PHP interactive CLI shell. Time() takes no arguments and returns the number of seconds since the Unix epoch. I’ll talk more on this later, but let’s ignore time zone issues for now and look at some time functions. All other time zones in the world are expressed as a positive or negative offsets from this time. Treating time in UTC and Unix time will make your life easier when you need to deal with time zones. UTC, also know by its full name Coordinated Universal Time, also referred to as GMT, and sometimes Zulu time, is the time at 0-degrees longitude. If you’re interested in a complete history of Unix time, check out the Unix time article on Wikipedia. Time is represented as an offset in the amount of seconds that have ticked away since midnight of January 1, 1970, UTC. Using the above function requires two inputs the MySQL-formatted date/datetime string and whether the string is to be returned as a date, or a datetime.Much of this article will work with Unix time, or POSIX or epoch time as it is otherwise known. Return Date($year, $month, $day, $hours, $minutes, $seconds) $month = (int) $match - 1 // Month conversion between indexes function JSdate($in,$type))/', $in, $match) In this case, I rebuild the string to the format expected by a Google Chart. Once the month-integer has been extracted from the date-string, a 1 is subtracted from its value and the date string can be built back up into any format. Understanding this approach is 9/10 of the battle. I should point out this post on StackOverflow that saved me time in figuring out the proper regex. For this reason, it makes sense to use regular expressions (regex) to extract the individual components of these strings. Worth mentioning is that although dates and datetimes are rigid data types within the database, once they’ve been queried and manipulated by PHP, they are plain old strings. As such, I want to make sure my conversion accommodates both data types. MySQL includes two timestamp data types that explicitly include a numbered month–date (YYYY-MM-DD) and datetime (YYYY-MM-DD hh:mm:ss). This technique supports that effort, but be advised that all we’re covering in this post is how to convert datetime to Javascript. This obviously causes problems, particularly when you’re creating a Google Chart that uses a database backend. ![]() On the other hand, Javascript uses a 0-based indexing rule for the month component of the date. For example, today, November 6th 2014, would be stored as. Why is this even necessary? Well, MySQL stores dates in a familiar manner (YYYY-MM-DD). However, this particular write-up just deals with converting MySQL’s datetime to Javascript, since Javascipt deals with recording months differently. This is part of an initiative of mine to document some of the challenges in using scientific data with Google Charts. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |