output days of the week via function - php

help me please
I have a code
<?php
// date on Russian
function getDateRus(){
$monthes = array(
1 => 'Января', 2 => 'Февраля', 3 => 'Марта', 4 => 'Апреля',
5 => 'Мая', 6 => 'Июня', 7 => 'Июля', 8 => 'Августа',
9 => 'Сентября', 10 => 'Октября', 11 => 'Ноября', 12 => 'Декабря'
);
return ( (int)date('d') . ' ' . $monthes[(date('n'))] . date(' Y'));
}
// day on Russian
function getDayRus(){
$days = array(
'Воскресенье', 'Понедельник', 'Вторник', 'Среда',
'Четверг', 'Пятница', 'Суббота'
);
return $days[(date('w'))];
}
echo "Today:" . getDateRus() . ", " . getDayRus();
?>
I need to put days off for a week ahead:
25 September, Monday
26 September, Tuesday
27 September, Wednesday
28 September, Thursday
29 September, Friday
30 September, Saturday
01 October, Sunday
I can print the dates for the week ahead, but without the Russian language.
how to deduce the dates for the week ahead and save the Russian language?

I changed your structure slightly and I made use of the powerful function DateTime
$monthes = array(
1 => 'Января', 2 => 'Февраля', 3 => 'Марта', 4 => 'Апреля',
5 => 'Мая', 6 => 'Июня', 7 => 'Июля', 8 => 'Августа',
9 => 'Сентября', 10 => 'Октября', 11 => 'Ноября', 12 => 'Декабря'
);
$days = array(
'Воскресенье', 'Понедельник', 'Вторник', 'Среда',
'Четверг', 'Пятница', 'Суббота'
);
$dArray = array();
$date = new DateTime();
for($i=0; $i<7;$i++){
$dArray[] = $date->format("d").' '.$monthes[$date->format("n")].','.$days[$date->format("w")];
$date->modify("+1 day");
}
print_r($dArray);
Output:
Array ( [0] => 25 Сентября,Понедельник [1] => 26 Сентября,Вторник [2] => 27 Сентября,Среда [3] => 28 Сентября,Четверг [4] => 29 Сентября,Пятница [5] => 30 Сентября,Суббота [6] => 01 Октября,Воскресенье )
You can probs achieve this better by using strftime and setlocale. That way you will not need your day/month arrays

Related

PHP strtotime +1 week error

I have a problem with strtotime and the following code (I want all calendar weaks in the given time range)
$all_dates = array();
$tempdate = '2014-01-01';
$enddate = '2015-03-01';
while (strtotime($tempdate) <= strtotime($enddate)) {
$all_dates[] = date('Y-W', strtotime($tempdate));
$tempdate = date('Y-m-d', strtotime($tempdate . '+1 week'));
}
var_dump($all_dates);
the var_dump produces the following output
array (
0 => '2014-01',
1 => '2014-02',
2 => '2014-03',
3 => '2014-04',
4 => '2014-05',
5 => '2014-06',
6 => '2014-07',
7 => '2014-08',
8 => '2014-09',
9 => '2014-10',
10 => '2014-11',
11 => '2014-12',
12 => '2014-13',
13 => '2014-14',
14 => '2014-15',
15 => '2014-16',
16 => '2014-17',
17 => '2014-18',
18 => '2014-19',
19 => '2014-20',
20 => '2014-21',
21 => '2014-22',
22 => '2014-23',
23 => '2014-24',
24 => '2014-25',
25 => '2014-26',
26 => '2014-27',
27 => '2014-28',
28 => '2014-29',
29 => '2014-30',
30 => '2014-31',
31 => '2014-32',
32 => '2014-33',
33 => '2014-34',
34 => '2014-35',
35 => '2014-36',
36 => '2014-37',
37 => '2014-38',
38 => '2014-39',
39 => '2014-40',
40 => '2014-41',
41 => '2014-42',
42 => '2014-43',
43 => '2014-44',
44 => '2014-45',
45 => '2014-46',
46 => '2014-47',
47 => '2014-48',
48 => '2014-49',
49 => '2014-50',
50 => '2014-51',
51 => '2014-52',
52 => '2014-01',
53 => '2015-02',
54 => '2015-03',
55 => '2015-04',
56 => '2015-05',
57 => '2015-06',
58 => '2015-07',
59 => '2015-08',
60 => '2015-09',
)
The Problem is entry number 52 with '2014-01', it should be '2015-01'. With the timerange 2015-01-01 till 2016-03-01 the result is ok. Is this a bug in strtotime function?
I'm using PHP version 5.5.10
Thanks for your help.
Change this line and use the o iso year:
$all_dates[] = date('o-W', strtotime($tempdate));
// ^
This has to do with ISO week numbers. Also, strtotime() is a bad way to do date math and PHP recommends against it (thanks to leap years and daylight savings). Use the DateTime() classes to do this with accuracy:
$startdate = new DateTime('2014-01-01');
$enddate = new DateTime('2015-03-01');
$interval = new DateInterval('P7D');
$period = new DatePeriod($startdate , $interval, $enddate);
foreach($period as $date) {
$all_dates[] = $date->format('o-W'); // o modifier for ISO year
}
var_dump($all_dates);
Demo

PHP: print all days between a given day and end of month

I don't want to return DATE (Y-m-d).
I need to print all days until end of month from a given day independently of the month or year.
I tried both [$array as $i] - [$array as $key] and didn't work.
$myday (for example = 19)
return $days
would result:
Array
[0] => 20
[1] => 21
[2] => 22
[3] => 23
...
[31] => 31 || [30] => 30 || [28] => 28
I would need each value for $days to compare each to another field.
Didn't try to use $myday as regular number instead of treating as date. And not use strtotime, mktime....
EDITING
Need something very simple like this one:
$output = array();
for ($i=$myday+1;$i<=31 || $i<=30 || $i<=28;$i++) {
$output[] = $i;
}
But print_r won't do it, I need to return as each value to use in different if conditions
This is easily done using DateTime(), DateInterval(), DatePeriod(), and relative date formats.
$start = (new DateTime())->setDate(date('Y'), date('m'), $myday + 1);
$end = new DateTime('first day of next month');
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
$days = array();
foreach($period as $date) {
$days[] = $date->format('d');
}
Results
Array
(
[0] => 20
[1] => 21
[2] => 22
[3] => 23
[4] => 24
[5] => 25
[6] => 26
[7] => 27
[8] => 28
[9] => 29
[10] => 30
[11] => 31
)
Demo

A list of the first and last date of the month based on a year

I want to create a function which outputs a list of the first date of the month and last date of the month based on the year, which is given as an argument.
function first_and_last_dates_of_month($year){
//code
}
first_and_last_dates_of_month(2014);
I want to have the output somthing like this:
2014-01-01
2014-01-31
2014-02-01
2014-02-28
2014-03-01
2014-03-31
2014-04-01
2014-04-30
2014-05-01
2014-05-31
2014-06-01
2014-06-30
2014-07-01
2014-07-31
2014-08-01
2014-08-31
2014-09-01
2014-09-30
2014-10-01
2014-10-31
2014-11-01
2014-11-30
2014-12-01
2014-12-31
Hope it will help you:
function first_and_last_dates_of_month($year=2014){
for ($i=1; $i<=12; $i++) {
$timestamp = strtotime(date("$i/01/y"));
echo "<br/>";
echo $first_second = date('Y-m-01', $timestamp);
echo "<br/>";
echo $last_second = date('Y-m-t', $timestamp);
}
}
first_and_last_dates_of_month(2014);
This is also a possibility.
$year = date('Y');
$array = array(
1 => ''.$year.'-01-01',
2 => ''.$year.'-01-'.cal_days_in_month(CAL_GREGORIAN, 1, $year),
3 => ''.$year.'-02-01',
4 => ''.$year.'-02-'.cal_days_in_month(CAL_GREGORIAN, 2, $year),
5 => ''.$year.'-03-01',
6 => ''.$year.'-03-'.cal_days_in_month(CAL_GREGORIAN, 3, $year),
7 => ''.$year.'-04-01',
8 => ''.$year.'-04-'.cal_days_in_month(CAL_GREGORIAN, 4, $year),
9 => ''.$year.'-05-01',
10 => ''.$year.'-05-'.cal_days_in_month(CAL_GREGORIAN, 5, $year),
11 => ''.$year.'-06-01',
12 => ''.$year.'-06-'.cal_days_in_month(CAL_GREGORIAN, 6, $year),
13 => ''.$year.'-07-01',
14 => ''.$year.'-07-'.cal_days_in_month(CAL_GREGORIAN, 7, $year),
15 => ''.$year.'-08-01',
16 => ''.$year.'-08-'.cal_days_in_month(CAL_GREGORIAN, 8, $year),
17 => ''.$year.'-09-01',
18 => ''.$year.'-09-'.cal_days_in_month(CAL_GREGORIAN, 9, $year),
19 => ''.$year.'-10-01',
20 => ''.$year.'-10-'.cal_days_in_month(CAL_GREGORIAN, 10, $year),
21 => ''.$year.'-11-01',
22 => ''.$year.'-11-'.cal_days_in_month(CAL_GREGORIAN, 11, $year),
23 => ''.$year.'-12-01',
24 => ''.$year.'-12-'.cal_days_in_month(CAL_GREGORIAN, 12, $year));
var_dump($array);

Year value error from PHP date() [duplicate]

This question already has answers here:
Display all the week numbers between two dates in PHP [duplicate]
(3 answers)
Closed 8 years ago.
Given a start and end date, I need to generate an array with year and week values.
For example:
Start Date: 2013-01-01
End Date: 2013-02-23
Generated Array:
Array ( [0] => Array ( [week] => 01 [year] => 2013 )
[1] => Array ( [week] => 02 [year] => 2013 )
[2] => Array ( [week] => 03 [year] => 2013 )
[3] => Array ( [week] => 04 [year] => 2013 )
[4] => Array ( [week] => 05 [year] => 2013 )
[5] => Array ( [week] => 06 [year] => 2013 )
[6] => Array ( [week] => 07 [year] => 2013 )
[7] => Array ( [week] => 08 [year] => 2013 ) )
Here is the code I'm using to generate this:
public static function getYearWeekRange($startdate, $enddate) {
$array = array();
$starttime = strtotime($startdate);
$endtime = strtotime($enddate);
while ($starttime <= $endtime) {
$year = date('Y', $starttime);
$week = date('W', $starttime);
$array[] = array('week' => $week, 'year' => $year);
$starttime = strtotime('+1 week', $starttime);
}
return $array;
}
My problem is that when I generate certain date ranges, I don't get the correct year value at the start of the 2013 year. For example:
Start Date: 2012-01-01
End Date: 2013-02-23
In this case, where it should have an subarray with year = 2013 and week = 01, it actually has it's year value equal to 2012.
If I were to switch the start date to 2013-01-05 for example, then there is no problem.
Can anyone offer a solution that would guarantee that my year and week values are always correct?
I was able to fix my problem using the following:
public static function getWeekRange($startdate, $enddate) {
$array = array();
$p = new DatePeriod(
new DateTime($startdate),
new DateInterval('P1W'),
new DateTime($enddate)
);
foreach ($p as $w) {
$array[] = array('year' => $w->format('o'), 'week' => $w->format('W'));
}
return $array;
}

PHP, multidimensional arrays: to intersected events with days of the month

I want to build a movie timetable. I have $array1 containing the movie titles and airing dates and times:
array =
0: array =
Title: string = American Beauty
Date: string = 25/09/2012
Time: string = 15:00 - 16:20
1: array =
Title: string = The Godfather
Date: string = 25/09/2012
Time: string = 16:20 - 18:20
2: array =
Title: string = Pulp Fiction
Date: string = 26/09/2012
Time: string = 15:00 - 16:20
And I have $array2 containing the days of the month grouped by Mondays, Tuesday s, Wednesday s, Thursday s and Fridays (no movies during the weekend)
Array
(
[1] => Array
(
[0] => 3
[1] => 10
[2] => 17
[3] => 24
[4] =>
)
[2] => Array
(
[0] => 4
[1] => 11
[2] => 18
[3] => 25
[4] =>
)
[3] => Array
(
[0] => 5
[1] => 12
[2] => 19
[3] => 26
[4] =>
)
[4] => Array
(
[0] => 6
[1] => 13
[2] => 20
[3] => 27
[4] =>
)
[5] => Array
(
[0] => 7
[1] => 14
[2] => 21
[3] => 28
[4] =>
)
)
I need to intersect these two arrays so I can print under day 25 the movie “American Beauty” also under day 25 “The Godfather” and under day 26 “Pulp Fiction”.
Meaning I need to print:
SEPTEMBER 2012
Monday Tuesday Wednesday ....
3 4 5
10 11 12
17 18 19
24 25 26
15:00-16:20 American Beauty 15:00-16:20 Pulp Fiction
16:20-18:20 The Godfather
My tries so far:
foreach( $array1 as $key => $value )
{
$theTime = $value['Time'];
$theTime = explode("/", $theTime );
$days[] = $theTime [0];
$months[] = $theTime [1];
}
So I have all the airing days in array $days but from here I don’t know how to follow or even if this approach is the correct one.
Here's how I get $array2:
$month = 9;
$year = 2012;
for ($i = 1; $i <= 31; $i++)
{
$timestamp = mktime(0, 0, 0, $month , $i, $year);
if (date("n", $timestamp) == $month )
{
$day = date("N", $timestamp);
// Monday 1 to Friday 5
if ($day == 1 OR $day <= 5) {
$array2[$day][] = date("j", $timestamp);
}
}
}
Please help, I’m stuck.
Thanks very much
Ok you will iterate over $array1 and parse the date of the movie.
Then, you will look inside array2 if your day is there
foreach($array1 as $movie){
$movieDate = new DateTime($movie);
//as you indexed $array2 with monday = 1 -> friday = 5 you can use the "w" format
if(isset($array2[$movieDate->format("w"))){
$array[$movieDate->format("j")][] = $movie;
}
}
I made some mutation in your output array, it becomes :
SEPTEMBER 2012
Monday Tuesday Wednesday ....
3=>[] 4 =>[] 5=>[]
10=>[] 11 =>[] 12=>[]
17=>[] 18 =>[] 19=>[]
24 =>[] 25=>[ 26 =>[
15:00-16:20 American Beauty, 15:00-16:20 Pulp Fiction]
16:20-18:20 The Godfather]

Categories