PHP - doubts with date("Y-m-d")

Asked

Viewed 93 times

1

Good afternoon person, someone could assist me in a doubt. I have this foreach that traverses an array and checks if the CURRENT DATE is equal to $value['Account']['data_expiration'] If it is he forwards an SMS informing that the account will expire.

foreach ($contasVencida as $key => $value) {
if (date("Y-m-d") == $value['Conta']['data_vencimento'] && !isset($_COOKIE['send_sms_Vayron'])){

    //Faz envio do SMS
    echo $this->element('NexmoMessage');

My doubt would be I would like to not warn the person on the day AND YES the day before or date("Y-m-d") -1 Day

In other words, you would have to take -1day from $value['Account']['expiration date'] because then I would do if the current date equals the maturity account -1Day

so that the SMS firing occurs one day before the account expires. account expires 19/03 the text triggered on 18/03

  • take a look at this link, maybe you can help in some way: https://celke.com.br/artigo/addr-e-subtrair-data-em-php

2 answers

0

Use strtotime:

date("Y-m-d", strtotime('+1 day', strtotime($date_raw)))

You can use the same logic (only with -1 day) to subtract from the due date instead of adding the current date.

  • So I found this tmb solution but look at the debug of how the date comes: app View Dashboards index.ctp (line 239) $value['Account']['data_expiration'] = '2020-03-20' When passing the date $dataFormatada = strtotime('-1 day', strtotime($value['Account']['data_expiration'])); app View Dashboards index.ctp (line 239) Debug: (int) 1584586800

  • The syntax is wrong

  • What would be the right way?

  • I have already made using the tutorial from top thanks friend

0


The way you’re doing it isn’t ideal. With each loop a new date is generated, however, as you are only working with day, month and year, hardly the last date will be different from the first (only if you run the script a few milliseconds before turning day). It is better to create the date before and always use the same variable

For this you can use the API Datetime, objects created from it have the method sub which makes it possible to reduce the date by a certain period, in this case the Perudium of 1 (one) Dia ('P1D'). Then just use the method format to return a string with the specified format

<?php

$date = new DateTime();

$date->sub(new DateInterval('P1D'));

$date = $date->format('Y-m-d');

foreach ($contasVencida as $key => $value) {
    if ($date == $value['Conta']['data_vencimento'] && !isset($_COOKIE['send_sms_Vayron'])){
        //Faz envio do SMS
        echo $this->element('NexmoMessage');

        // ...
  • it’s just worked out thanks]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.