开发者

Reformat date before echo PHP

开发者 https://www.devze.com 2023-03-17 23:22 出处:网络
We all know dates are stored as YYYY-MM-DD. I have a search box that brings up user information including birth dates etc.. how can I reformat the string before it actually echoes it below??:

We all know dates are stored as YYYY-MM-DD. I have a search box that brings up user information including birth dates etc.. how can I reformat the string before it actually echoes it below??:

开发者_开发知识库
   echo '<br /> Exp. Date: '   .$row['exp_date'];


echo '<br /> Exp. Date: ' . date(DATE_RSS, strtotime(row['exp_date']));

Where DATE_RSS is whatever date format you want.


Your looking for the php function called date().

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

For the $format in the style of your example would be;

  • y - As it's the four digit year.
  • m - As it's always a two digit month, with leading zero.
  • d - As it's always a two digit day, with leading zero.

The php date function does not parse hythens, so you can just add them like so to make the format parameter correct for your function. $format = 'y-m-d';.

Now $timestamp paramater expects the seconds since the Unix Epoch. As we think your currently formatted as a string, we can convert this to seconds with the strtotime function. For that it's as simple as doing this, $timestamp = strtotime($row['exp_date']);

Puting this all togeater into one simple function call and outputing the results like you asked is the following code sample.

echo '<br /> Exp. Date: ' . date('y-m-d', strtotime($row['exp_date']));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号