Я знаю, что здесь есть несколько ответов, но это то, что я придумал. Это относится только к значениям MySQL DATETIME в соответствии с исходным вопросом, на который я отвечал. Массив $ a нуждается в некоторой работе. Я приветствую комментарии о том, как улучшить. Вызвать как:
echo time_elapsed_string ('2014-11-14 09:42:28');
function time_elapsed_string($ptime)
{
// Past time as MySQL DATETIME value
$ptime = strtotime($ptime);
// Current time as MySQL DATETIME value
$csqltime = date('Y-m-d H:i:s');
// Current time as Unix timestamp
$ctime = strtotime($csqltime);
// Elapsed time
$etime = $ctime - $ptime;
// If no elapsed time, return 0
if ($etime < 1){
return '0 seconds';
}
$a = array( 365 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
$a_plural = array( 'year' => 'years',
'month' => 'months',
'day' => 'days',
'hour' => 'hours',
'minute' => 'minutes',
'second' => 'seconds'
);
foreach ($a as $secs => $str){
// Divide elapsed time by seconds
$d = $etime / $secs;
if ($d >= 1){
// Round to the next lowest integer
$r = floor($d);
// Calculate time to remove from elapsed time
$rtime = $r * $secs;
// Recalculate and store elapsed time for next loop
if(($etime - $rtime) < 0){
$etime -= ($r - 1) * $secs;
}
else{
$etime -= $rtime;
}
// Create string to return
$estring = $estring . $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ';
}
}
return $estring . ' ago';
}
Проблема, с которой я столкнулся, была неверной основой для файла. Но так как мне нужно было изменить его динамически, я не мог просто установить его в настройках src
. К счастью, я мог легко изменить его в функции dest
, где у меня есть доступный путь:
.pipe(gulp.dest((fileVinyl) => {
//changing the base so that it will include the whole path to the file,
//excluding the filename
fileVinyl.base = fileVinyl.path.replace(/[^\/]*js$/, '')
//outputing the file to a /public/ folder instead of /source/,
//keeping the structure
return path.relative(process.cwd(), fileVinyl.path.replace('/source/', '/public/').replace(/[^\/]*js$/, ''))
}))
используйте опцию base
в gulp.src
следующим образом:
gulp.src('your_file.js', {base: 'your_base_path'})
.pipe(/*...*/)
в вашем случае значение base
должно быть lib/modules
например:
gulp.src('lib/modules/**/*.js', {base: 'lib/modules'})
.pipe(/*...*/)
.pipe(gulp.dest('lib/modules'))
это создаст то, что вы хотите:
lib
- modules
- module1
- public
- js
- file.js
Эта опция говорит gulp игнорировать базовую часть пути и обрабатывать оставшуюся часть пути как относительную и, следовательно, реплицировать это в десте