forked from schmiddim/cigarette-stop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.php
29 lines (24 loc) · 1.04 KB
/
cli.php
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
<?php
/**
*
* example php cli.php 2016-09-19 30 0.285714286
*/
if (4 !== $argc) {
echo 'usage: php ' . __FILE__ . ' [date-stopped (Y-m-D)] [cigarettes per day] [price / cigarette]' . PHP_EOL;
exit;
}
function getSmokingString(\DateTime $dateStopped, $cigarettesPerDay=0, $pricePerCigarette=0)
{
$dateInterval = (new \DateTime('now'))->diff($dateStopped);
$daysWithoutSmoking =intval( $dateInterval->format('%a'));
$hoursToday = intval($dateInterval->format('%h'));
$cigarettesNotSmokedToday = round($cigarettesPerDay / 24 * $hoursToday,0);
$cigarettesNotSmoked = $daysWithoutSmoking * $cigarettesPerDay + $cigarettesNotSmokedToday;
$moneySaved = $pricePerCigarette * $cigarettesNotSmoked;
return sprintf('You stopped smoking %d days ago. Saved %d Euro and you didnt\'t smoked %d cigarettes', $daysWithoutSmoking, $moneySaved, $cigarettesNotSmoked);
}
$dateStopped = new \DateTime($argv[1]);
$cigarettesPerDay = $argv[2];
$pricePerCigarette = $argv[3];
echo getSmokingString($dateStopped, $cigarettesPerDay, $pricePerCigarette);
echo PHP_EOL;