Giờ tiếng Anh tính theo giây - Thủ thuật CSS

Anonim

Chỉ cần nhập thời gian bạn muốn chuyển đổi thành giây trong tiếng Anh (ví dụ: “1 giờ 30 phút”) và nó sẽ được chuyển đổi thành số nguyên giây (ví dụ: 5400). Cảm ơn Baylor Rae.

function time2seconds($time) ( preg_match_all('/(\d+ (a-z)+)/', $time, $matches); $matches = $matches(0); $formats = array(); foreach ($matches as $format) ( preg_match('/(\d+)\s?((a-z)+)/', $format, $f); $time = $f(1); $type = $f(2); $formats($type) = $time; ) $output = array( 'years' => 0, 'months' => 0, 'days' => 0, 'hours' => 0, 'minutes' => 0, 'seconds' => 0 ); foreach ($formats as $format => $time) ( if( $time == 0 ) continue; switch ($format) ( case 'year' : case 'years' : $output('years') = $time * 12 * 30 * 24 * 60 * 60; break; case 'month' : case 'months' : $output('months') = $time * 30 * 24 * 60 * 60; break; case 'day' : case 'days' : $output('days') = $time * 24 * 60 * 60; break; case 'hour' : case 'hours' : $output('hours') = $time * 60 * 60; break; case 'minute' : case 'minutes' : $output('minutes') = $time * 60; break; case 'second' : case 'seconds' : $output('seconds') = $time; break; ) ) return $output('years') + $output('months') + $output('days') + $output('hours') + $output('minutes') + $output('seconds'); )

Cách sử dụng đơn giản

Biểu mẫu gửi “thời gian”:

 Time
Test!

Nếu “thời gian” được đặt, hãy sử dụng hàm và lặp lại những gì được trả về:

if (isset($_POST)) ( echo time2seconds($_POST('time')); )