S
S
Sama Samsonov2018-04-06 12:32:45
PHP
Sama Samsonov, 2018-04-06 12:32:45

How to convert date?

<?php

function days_in_month($year, $month) {
return round((mktime(0, 0, 0, $month+1, 1, $year) - mktime(0, 0, 0, $month, 1, $year)) / 86400);
}


$days_in_mon = days_in_month(2017, 06);

//var_dump($days_in_mon);
//die;

for($day = 1; $day <= $days_in_mon; $day++){
     $day."<br>";
}

?>

the loop works and outputs from 1 to 30 (1,2,3,4,5,6,7,8,9,10...30)
how can I convert the output in this format (01:06:2017, 02:06 :2017, 03:06:2017, 04:06:2017, 05:06:2017, 06:06:2017, 07:06:2017, 08:06:2017, 09:06:2017, 10:06:2017 ... 30:06:2017)
i.e. $day."<br>";how to fix

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Hog, 2018-04-06
@samuser

function getPeriod($year, $month)
{
    $date = (new DateTimeImmutable())->setDate($year, $month, 1);
    $interval = new DateInterval('P1D');

    return new DatePeriod($date, $interval, $date->modify('+1 month'));
}

$period = getPeriod(2017, 6);

foreach ($period as $date) {
    // формат можете указать любой
    echo $date->format('d:m:Y') . '<br>';
}

K
Konstantin Tolmachev, 2018-04-06
@dark_tke

date_format()
Well, or a crutch:
$str_day = ($day <10) ? "0".$day : $day;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question