mirror of
https://github.com/codeguy/php-the-right-way.git
synced 2025-08-07 06:26:45 +02:00
@@ -27,11 +27,11 @@ Let's write a simple "Hello, $name" CLI program. To try it out, create a file na
|
|||||||
{% highlight php %}
|
{% highlight php %}
|
||||||
<?php
|
<?php
|
||||||
if ($argc !== 2) {
|
if ($argc !== 2) {
|
||||||
echo "Usage: php hello.php <name>.\n";
|
echo "Usage: php hello.php <name>" . PHP_EOL;
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
$name = $argv[1];
|
$name = $argv[1];
|
||||||
echo "Hello, $name\n";
|
echo "Hello, $name" . PHP_EOL;
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
|
||||||
PHP sets up two special variables based on the arguments your script is run with. [`$argc`][argc] is an integer
|
PHP sets up two special variables based on the arguments your script is run with. [`$argc`][argc] is an integer
|
||||||
|
@@ -19,7 +19,7 @@ output.
|
|||||||
$raw = '22. 11. 1968';
|
$raw = '22. 11. 1968';
|
||||||
$start = DateTime::createFromFormat('d. m. Y', $raw);
|
$start = DateTime::createFromFormat('d. m. Y', $raw);
|
||||||
|
|
||||||
echo 'Start date: ' . $start->format('Y-m-d') . "\n";
|
echo 'Start date: ' . $start->format('Y-m-d') . PHP_EOL;
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
|
||||||
Calculating with DateTime is possible with the DateInterval class. DateTime has methods like `add()` and `sub()` that
|
Calculating with DateTime is possible with the DateInterval class. DateTime has methods like `add()` and `sub()` that
|
||||||
@@ -34,7 +34,7 @@ $end = clone $start;
|
|||||||
$end->add(new DateInterval('P1M6D'));
|
$end->add(new DateInterval('P1M6D'));
|
||||||
|
|
||||||
$diff = $end->diff($start);
|
$diff = $end->diff($start);
|
||||||
echo 'Difference: ' . $diff->format('%m month, %d days (total: %a days)') . "\n";
|
echo 'Difference: ' . $diff->format('%m month, %d days (total: %a days)') . PHP_EOL;
|
||||||
// Difference: 1 month, 6 days (total: 37 days)
|
// Difference: 1 month, 6 days (total: 37 days)
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
|
||||||
@@ -43,8 +43,7 @@ You can use standard comparisons on DateTime objects:
|
|||||||
{% highlight php %}
|
{% highlight php %}
|
||||||
<?php
|
<?php
|
||||||
if ($start < $end) {
|
if ($start < $end) {
|
||||||
echo "Start is before the end!\n";
|
echo "Start is before the end!" . PHP_EOL;}
|
||||||
}
|
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
|
||||||
One last example to demonstrate the DatePeriod class. It is used to iterate over recurring events. It can take two
|
One last example to demonstrate the DatePeriod class. It is used to iterate over recurring events. It can take two
|
||||||
|
Reference in New Issue
Block a user