Get the day number from a date [duplicate] - php

This question already has answers here:
PHP - How to get year, month, day from time string
(5 answers)
Closed 7 years ago.
I want to get the day number of a date.
For example my date is: 2015-12-31, it should return 365 and 2015-01-1 would be 1.
How can I do that?
EDIT: date('z') does not work for me, I dont want the current date

Use strtotime() — to parse about any English textual datetime description into a Unix timestamp. And getdate() — Get date/time information.
<?php
$day = getdate(strtotime("2015-12-31"));
echo $day['yday']+1; // output 365
?>

Try
<?php
$today = getdate(strtotime('2015-12-31'));
echo '<pre>';
print_r($today['yday'] + 1);
?>
Read GetDate

You can try it.
<?php
$day = 31;
$mon = 12;
$year = 2015;
$sum = 0;
$days = 0;
$month_day = array(31,28,31,30,31,30,31,31,30,31,30,31);
for ( $i = 0; $i < $mon; $i++){
$sum += $month_day[$i];
}
$days = $sum - ($month_day[$mon-1] - $day);
echo "$days";
?>
You may get day, mon, year from your expected date and make it integer. You should check the year is leap year or not. It may vary one day if it is a leap year. I think you shall do it.

Related

Converting day of week to dates for the current month with MYSQL & PHP

Spent a while searching and couldn't find any answers. Really simple question I hope.
In my database I have a string column dayOfWeek.
I use this to track what day of week weekly events are scheduled for. Usually I match the dayOfWeek to the current day using PHP and display the correct events.
What I want to do is show a calendar into the future which makes a list of the future dates of the weekly recurring event.
For example, if the event takes place on Tuesday, and Feb 2017 has Tuesdays on the 7th, 14th, 21st, and 28th. I'd like to use PHP to display as such.
I know strtotime() is used to do the opposite, find a day from the date, but can the opposite be done easily?
Sorry if my question is poorly worded or missing information.
USE CASE FOR FEB 2017 TUESDAYS
$n=date('t',strtotime("2017-02-01")); // find no of days in this month
$dates=array();
for ($i=1;$i<=$n;$i++){
$day=date("D",strtotime("2017-02-".$i)); //find weekdays
if($day=="Tue"){
$dates[]="2017-02-".$i;
}
}
print_r($dates);
http://phpfiddle.org/main/code/s8wq-xx44
You could do this:
// The day of the week.
$dayOfEvent = row['day'];
// Say, you wanted the next 5 weeks.
$numberOfWeeks = 5;
$tempDate = date(U);
$counter = 0;
do{
$tempDate = $tempDate + 86,164;
if(date('D', $tempDay) == 'Tue'){
echo date('l jS \of F Y', $tempDay);
$counter++;
}
}while($counter < $numberOfWeeks);
First try to find the date for the day in current week. After that keep adding 7 days multiples to it.
<?php
// $timestamp = now();
for ($i=0; $i < 7; $i++) {
$days = $i." days";
$t = date('Y-m-d', strtotime($days));
$dayOfTheDay = date("D",strtotime($t));
if ( $dayOfTheDay == day from table )
{
$current_date = $t;
break;
}
}
for ($i=0; $i < 200; $i++) {
in this loop add 7 days to current_date
}
?>

how to display dates from starting to end date from current month using php?

I can display dates from start to end date from stored data in mysql, but I want to display current month dates from 1st to end date of this month in form of
1
2
3
4
.
.
.
.
.
31
Is this possible?
Refer to PHP cal_days_in_month
As explained here
This function will return the number of days in the month of year for the specified calendar.
int cal_days_in_month ( int $calendar , int $month , int $year )
And an example:
$number = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31
echo "There were {$number} days in August 2003";
Use a loop to display a count of the number of days
For the PHP part, this might help you:
// Get the current date
$today = getdate();
// Get the number of days in current month
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $today['mon'], $today['year']);
// Print the dates
for ($i = 1; $i <= $days_in_month; $i++) {
echo ' ' . $i;
}
Styling and output is another task, this is just to get you started.
yes. it is possible.
please, use below php code. it can work for php 4.1 and higher.
<?php
$number = cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y'));
for($i=1;$i<=$number;$i++)
echo $i.'<br>';
?>
If you want all days in the month, try this loop where date("t") give you the numerical last day of the month, and we know the first day is always 1.
$last = date("t");
for($i=1; $i<= $last; $i++) echo "$i ";

How to get all dates between two dates [duplicate]

This question already has answers here:
PHP: Return all dates between two dates in an array [duplicate]
(26 answers)
Closed 7 years ago.
I have check-in column and check-out column in my mysql table. I am posting this dates with UI-Datepicker.
eg:
I am trying to block the dates in the datepicker. I have dates like this in database
check in = 2015-06-15
check out = 2015-06-20
Now i want to get dates like this
2015-06-15,
2015-06-16,
2015-06-17,
2015-06-18,
2015-06-19,
2015-06-20
How to get dates like above I mentioned. So please help me and resolve my problem.
<?php
$date_from = strtotime("10 September 2000");
$date_to = strtotime("15 September 2000");
function list_days($date_from,$date_to){
$arr_days = array();
$day_passed = ($date_to - $date_from); //seconds
$day_passed = ($day_passed/86400); //days
$counter = 1;
$day_to_display = $date_from;
while($counter < $day_passed){
$day_to_display += 86400;
//echo date("F j, Y \n", $day_to_display);
$arr_days[] = date('o-m-d',$day_to_display);
$counter++;
}
return $arr_days;
}
print_r(list_days($date_from,$date_to));
?>

Select all the dates between two chosen dates [duplicate]

This question already has answers here:
PHP: Return all dates between two dates in an array [duplicate]
(26 answers)
Closed 8 years ago.
I am building an app on php,mysql which is related to events. This is a specific feature where the user will repeat an activity/event . The user will select a start date and an end date . Now i am trying to figure out how to calculate the dates between those two selected dates. I am using jquery's datepicker for date selection .
$start_date = date("Y-m-d", strtotime($_POST['start_date']));
$end_date = date("Y-m-d", strtotime($_POST['end_date']));
Datediff doesn't seem to do the work as it only gives the difference between two dates . I want all the dates between those two dates .
Would be great if someone could advice as per how to go about this .
By this way You can fetch the dates between two dates
$start_date = strtotime($_POST['start_date']);
$end_date = strtotime($_POST['end_date']);
// Loop between timestamps, 24 hours at a time
for ( $i = $start_date ; $i <= $end_date ; $i = $i + 86400 ) {
echo date( 'Y-m-d', $i )."<br>";
}
function createDateRangeArray($strDateFrom,$strDateTo)
{
$aryRange=array();
$iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2), substr($strDateFrom,8,2),substr($strDateFrom,0,4));
$iDateTo=mktime(1,0,0,substr($strDateTo,5,2), substr($strDateTo,8,2),substr($strDateTo,0,4));
if ($iDateTo>=$iDateFrom)
{
array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
while ($iDateFrom<$iDateTo)
{
$iDateFrom+=86400; // add 24 hours
array_push($aryRange,date('Y-m-d',$iDateFrom));
}
}
return $aryRange;
}
Found the above solution and it works great. Thanks everyone for the time and advice.
I would recommend you to use DateTime objects
date_default_timezone_set("Europe/Istanbul");
$dt = new DateTime("10-01-2014");
$diff = $dt->diff(new Datetime("12-01-2014"));
for($i = 1; $i <= $diff->days; $i++) {
$nextdate = $dt->modify("+1 day");
echo $nextdate->format("d-m-Y"); echo "\n";
}

strtotime() seems bugged with a special date [duplicate]

This question already has answers here:
How do I add 24 hours to a unix timestamp in php?
(7 answers)
Closed 8 years ago.
I'm writing a function which calculates the days between two dates.
Strangely I have a strange behaviour for one special day. Here is a part of my code:
$startTimestamp = strtotime('25-10-2014');
$endTimestamp = strtotime('28-10-2014');
for($i = $startTimestamp; $i <= $endTimestamp; $i = $i + (60 * 60 * 24))
{
echo date("d-m-Y", $i).'<br />';
}
This example gives me this result:
25-10-2014
26-10-2014
26-10-2014
27-10-2014
I don't know why the date "26-10-2014" appears two times.
If I try with another dates like:
$startTimestamp = strtotime('25-11-2014');
$endTimestamp = strtotime('28-11-2014');
The result is correct:
25-11-2014
26-11-2014
27-11-2014
28-11-2014
It is probably because of the DST (daylight saving time) in the timezone your PHP is configured to.
This mean, if you jump forward 24h, you'll still be the same day, because the day is 25h long.
I would use the DateTime class:
$startTime = new DateTime('25-10-2014');
$endTime = new DateTime('28-10-2014');
do {
echo $startTime->format('d-m-Y'), PHP_EOL;
} while($startTime->modify('+1 day') <= $endTime);
Note that you can use the <= operator when comparing DateTime objects. Also the DateTime::modify() method is aware of daylight saving time issues. (Note that there is a change in daylight saving time on Oct 26)

Categories