Nasıl PHP kullanarak bir ay içinde ilk ve son tarih bulabilirim? Örneğin, bugün 21 Nisan 2010 olup; Ben 1 Nisan 2010 ve 30 Nisan 2010 bulmak istiyorum.
En kolay yolu, bir zaman damgası çıkarılan olanlar ile kodlanmış değerleri karışımı sağlar ki, date kullanmaktır. Bir zaman damgası vermezlerse, o anki tarih ve saati varsayar.
// Current timestamp is assumed, so these find first and last day of THIS month
$first_day_this_month = date('m-01-Y'); // hard-coded '01' for first day
$last_day_this_month = date('m-t-Y');
// With timestamp, this gets last day of April 2010
$last_day_april_2010 = date('m-t-Y', strtotime('April 21, 2010'));
date() özel sembollerle için, 'm-t-Y' gibi, verdi dize arar ve onun damgası değerleri ile değiştirir. Yani biz damgası istediğiniz değerleri ve biçimlendirmeyi ayıklamak için bu simgeleri kullanabilirsiniz. Yukarıdaki örneklerde:
Y damgası ('2010 ') sizi 4 basamaklı yıl verirm (''04) önde gelen sıfır, size zaman damgası sayısal ay verirt (''30) zaman damgası adlı aydaki gün sayısını verirBu yaratıcı olabilir. Örneğin, bir ayın ilk ve son saniye almak için:
$timestamp = strtotime('February 2012');
$first_second = date('m-01-Y 00:00:00', $timestamp);
$last_second = date('m-t-Y 12:59:59', $timestamp); // A leap year!
http://php.net/manual/en/function.date.php, diğer semboller ve daha fazla bilgi için bkz.
Sen orada kaç gün bir ay içinde bulmak için tarih işlevini kullanabilirsiniz.
// Get the timestamp for the date/month in question.
$ts = strtotime('April 2010');
echo date('t', $ts);
// Result: 30, therefore, April 30, 2010 is the last day of that month.
Umut olur.
EDIT: Luis'in cevabını okuduktan sonra, size doğru biçimde (YY-mm-dd) bunu isteyebilirsiniz aklıma geldi. Bu açık olabilir, ama söz zarar vermez:
// After the above code
echo date('Y-m-t', $ts);
Basit bir
Referans - http://www.php.net/manual/en/function.date.php
<?php
echo 'First Date = ' . date('Y-m-01') . '<br />';
echo 'Last Date = ' . date('Y-m-t') . '<br />';
?>
Bu size ayın son günü verecek:
function lastday($month = '', $year = '') {
if (empty($month)) {
$month = date('m');
}
if (empty($year)) {
$year = date('Y');
}
$result = strtotime("{$year}-{$month}-01");
$result = strtotime('-1 second', strtotime('+1 month', $result));
return date('Y-m-d', $result);
}
Ve ilk günü:
function firstDay($month = '', $year = '')
{
if (empty($month)) {
$month = date('m');
}
if (empty($year)) {
$year = date('Y');
}
$result = strtotime("{$year}-{$month}-01");
return date('Y-m-d', $result);
}