문제

How could I convert a unix timestamp , i.e. from time(), into a m-d-Y string using php?

도움이 되었습니까?

해결책

use date()

string date ( string $format [, int $timestamp ] )

Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().

다른 팁

The very first example in the mktime manual entry shows you such a function. No good developer can be made of one who ignores manual lookup.

The function localtime() is the inverse of PHP's mktime(). These simply execute the underlying ANSI-C functions localtime and mktime in a PHP-ish manner. Be careful, because the mappings of broken-down time are not symmetric. Specially, localtime() returns the values from the underlying ANSI-C localtime() without modification, but mktime() arguments are more PHP-ish:

  1. The arguments are in a more human readable order, more in line with the formats supported by the PHP function strtotime(). In C, these arguments are passed in via a struct, but are general thought of in broken-down time order, as are the numerically indexed return values of PHP's localtime()/
  2. The month argument for PHP's mktime() takes values from 1 to 12. But PHP's localtime() return 0 to 11, as is normal with broken-down time. Remember to add/subtract 1 as appropriate.
  3. PHP's mktime() takes years values of 0-69 to represent 2000 to 2069, while taking 70-100 to represent 1970 to 2000. PHP's localtime() returns the years since 1900 (0 to 138 represent 1900 to 2038, the range of the 32-bit broken-down time). So unless your application needs to track times before 2000, add/subtract 100 as appropriate and pretend the 70's never existed (that also takes care of the problems with Disco).
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top