SORU
19 Ocak 2010, Salı


DateTime nesnesi olarak php date_modify kullanarak geçerli ayın ilk günü

Pazartesi bu hafta alabilirim:

$monday = date_create()->modify('this Monday');

Aynı kolaylığı bu ayın 1 ile almak istiyorum. Bunu nasıl elde edebilirsiniz?

Teşekkürler

CEVAP
19 Ocak 2010, Salı


PHP 5.3 çalışmayı gerektirir ("ilk gün" PHP 5.3 tanıtıldı). Aksi takdirde yukarıdaki örnekte bunu yapmak için tek yoldur:

<?php
    // First day of this month
    $d = new DateTime('first day of this month');
    echo $d->format('jS, F Y');

    // First day of a specific month
    $d = new DateTime('2010-01-19');
    $d->modify('first day of this month');
    echo $d->format('jS, F Y');

    // alternatively...
    echo date_create('2010-01-19')
      ->modify('first day of this month')
      ->format('jS, F Y');

PHP 5.4 yapabilirsiniz:

<?php
    // First day of this month
    echo (new DateTime('first day of this month'))->format('jS, F Y');

    echo (new DateTime('2010-01-19'))
      ->modify('first day of this month')
      ->format('jS, F Y');

Eğer kısa ve öz bir şekilde bunu yapmak, ve zaten yıl ve ay için, sayısal olarak tercih ederseniz, date() kullanabilirsiniz:

<?php
    echo date('Y-m-01'); // first day of this month
    echo date("$year-$month-01"); // first day of a month chosen by you

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • andony5589

    andony5589

    7 Aralık 2011
  • DancingIsAPassion

    DancingIsAPa

    29 AĞUSTOS 2009
  • LaKe Lightroom Tutorials

    LaKe Lightro

    22 Temmuz 2014