Skip to content

Instantly share code, notes, and snippets.

@timmipetit
Created April 17, 2012 14:29
Show Gist options
  • Save timmipetit/2406317 to your computer and use it in GitHub Desktop.
Save timmipetit/2406317 to your computer and use it in GitHub Desktop.
Search dates (yyyy-mm-dd) in a text file and increase these dates with n days (defaults to 1)
<?php
if($argc < 2){
echo "Usage: " . $argv[0] . " FILE [NUMDAYS]\n";
return(1);
}
// Default to add 1 day
$daysToAdd = ($argc == 3) ? intval($argv[2]) : 1;
$text = file_get_contents($argv[1]);
$increase = function($match) use ($daysToAdd){
try {
$date = new DateTime($match[0]);
} catch (Exception $e) {
// Assume it was an invalid date, so just return the match
return $match[0];
}
if($daysToAdd > 0){
$date->add(new DateInterval('P'. $daysToAdd . 'D'));
} else {
$date->sub(new DateInterval('P'. abs($daysToAdd) . 'D'));
}
return $date->format('Y-m-d');
};
$replaced = preg_replace_callback('|(\d{4}-\d{2}-)(\d{2})|', $increase,$text);
file_put_contents($argv[1], $replaced);
return(0);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment