简体   繁体   中英

The first day and the last day of the month

Can someone help me with this, I need get the following values:

  • $startTime must be the first of a month with 00:00:00 , otherwise

    invalid time

  • end_date from $_GET must always be the first of a month with time 00:00:00 , otherwise invalid date

  • $defaultEndTime must be the first of current month with time 00:00:00
  • $endTime must be the first of a month with 00:00:00 , otherwise

    invalid time

  • difference between $startTime and $endTime must be exactly one month

I tried:

    $end_date=new DateTime();
    $end_date->format('Y-m-js 00:00:00');

I tried to use datedif to get the different startTime end endTime but I don't know how to get one month.

To get first day and last day of month

    $date = '2018-01-11';
    // First day with 00:00:00
    echo date('Y-m-01 00:00:00', strtotime($date ));
    echo '<br>';
    // Last day with 00:00:00
    echo date('Y-m-t 00:00:00', strtotime($date ));

Output:

2018-01-01 00:00:00
2018-01-31 00:00:00

=> Try this code..

<?php

echo date("Y-m-d 00:00:00", strtotime('first day of this month'));

echo "<br/>";

echo date("Y-m-d 00:00:00", strtotime('last day of this month'));

?>

Demo :- https://eval.in/933378

 $startDate = date('01-m-Y 00:00:00',strtotime('this month')); // First day of current month

 echo $startDate;

 $endDate =  date("t-m-Y 00:00:00", strtotime($startDate ));// Last date of current month. 
 //'t' provide last day of month means if, it is Jan then get 31 
 //and also if current month is Feb then t provide 28.It's happend same as other months.

 echo $endDate ;

Lets Today is 11-01-2018. Then output :

  01-01-2018 00:00:00
  31-01-2018 00:00:00

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM