PHP converts date format to format xx days ago_PHP tutorial

WBOY
Release: 2016-07-13 09:57:07
Original
799 people have browsed it

php converts the date format into the format of xx days ago

This code can format the time into the format of 3 days ago, 5 seconds ago, and 2 years ago

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

// convert a date into a string that tells how long ago

// that date was.... eg: 2 days ago, 3 minutes ago.

function ago($d) {

$c = getdate();

$p = array('year', 'mon', 'mday', 'hours', 'minutes', 'seconds');

$display = array('year', 'month', 'day', 'hour', 'minute', 'second');

$factor = array(0, 12, 30, 24, 60, 60);

$d = datetoarr($d);

for ($w = 0; $w < 6; $w ) {

if ($w > 0) {

$c[$p[$w]] = $c[$p[$w-1]] * $factor[$w];

$d[$p[$w]] = $d[$p[$w-1]] * $factor[$w];

}

if ($c[$p[$w]] - $d[$p[$w]] > 1) {

return ($c[$p[$w]] - $d[$p[$w]]).' '.$display[$w].'s ago';

}

}

return '';

}

// you can replace this if need be.

// This converts my dates returned from a mysql date string

// into an array object similar to that returned by getdate().

function datetoarr($d) {

preg_match("/([0-9]{4})(\-)([0-9]{2})(\-)([0-9]{2})([0-9]{2})(\:)([0-9]{2})(\:)([0-9]{2})/",$d,$matches);

return array(

'seconds' => $matches[10],

'minutes' => $matches[8],

'hours' => $matches[6],

'mday' => $matches[5],

'mon' => $matches[3],

'year' => $matches[1],

);

}

1 2 3

4

6 7 8 9 10
11 12
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
// convert a date into a string that tells how long ago // that date was.... eg: 2 days ago, 3 minutes ago. function ago($d) { $c = getdate(); $p = array('year', 'mon', 'mday', 'hours', 'minutes', 'seconds'); $display = array('year', 'month', 'day', 'hour', 'minute', 'second'); $factor = array(0, 12, 30, 24, 60, 60); $d = datetoarr($d); for ($w = 0; $w < 6; $w ) {<🎜> <🎜>if ($w > 0) { $c[$p[$w]] = $c[$p[$w-1]] * $factor[$w]; $d[$p[$w]] = $d[$p[$w-1]] * $factor[$w]; } if ($c[$p[$w]] - $d[$p[$w]] > 1) { return ($c[$p[$w]] - $d[$p[$w]]).' '.$display[$w].'s ago'; } } return ''; } // you can replace this if need be. // This converts my dates returned from a mysql date string // into an array object similar to that returned by getdate(). function datetoarr($d) { preg_match("/([0-9]{4})(\-)([0-9]{2})(\-)([0-9]{2})([0-9 ]{2})(\:)([0-9]{2})(\:)([0-9]{2})/",$d,$matches); return array( 'seconds' => $matches[10], 'minutes' => $matches[8], 'hours' => $matches[6], 'mday' => $matches[5], 'mon' => $matches[3], 'year' => $matches[1], ); }
http://www.bkjia.com/PHPjc/985138.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/985138.htmlTechArticlephp converts the date format into the format of xx days ago. This code can format the time into 3 days ago, 5 seconds ago, 2 years ago form 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template