Now, I'm try to change date format from year-month-day to month/day/year form array.
I have an array that contains multiple dates in array i want to change each date format to year-month-day to month/day/year. Please check below array that contains multiple dates. Please help me if know something about that.
Array
(
[0] => 2018-05-19
[1] => 2018-05-20
[2] => 2018-05-21
[3] => 2018-05-22
[4] => 2018-05-23
[5] => 2018-05-24
[6] => 2018-05-25
[7] => 2018-05-26
[8] => 2018-05-27
[9] => 2018-05-28
[10] => 2018-05-29
[11] => 2018-05-30
[12] => 2018-05-31
[13] => 2018-06-04
[14] => 2018-06-05
[15] => 2018-06-08
[16] => 2018-06-09
[17] => 2018-06-10
[18] => 2018-06-13
[19] => 2018-06-14
[20] => 2018-06-15
[21] => 2018-06-17
[22] => 2018-06-20
[23] => 2018-06-21
[24] => 2018-06-24
[25] => 2018-06-28
[26] => 2018-06-29
[27] => 2018-07-02
[28] => 2018-07-05
[29] => 2018-07-11
[30] => 2018-07-12
[31] => 2018-07-15
[32] => 2018-07-16
[33] => 2018-07-17
[34] => 2018-07-20
[35] => 2018-07-23
[36] => 2018-07-24
[37] => 2018-07-25
[38] => 2018-07-26
[39] => 2018-07-27
[40] => 2018-07-29
[41] => 2018-08-07
[42] => 2018-08-08
[43] => 2018-08-10
[44] => 2018-08-11
[45] => 2018-08-12
[46] => 2018-08-13
[47] => 2018-08-15
[48] => 2018-08-17
[49] => 2018-08-18
[50] => 2018-08-19
[51] => 2018-08-20
[52] => 2018-08-21
[53] => 2018-08-25
[54] => 2018-08-26
[55] => 2018-08-27
[56] => 2018-08-28
[57] => 2018-08-29
[58] => 2018-08-30
)
I have an array that contains multiple dates in array i want to change each date format to year-month-day to month/day/year. Please check below array that contains multiple dates. Please help me if know something about that.
Here you go:
<?php
$currentDates = [
'2018-05-19',
'2018-05-20',
'2018-05-21',
'2018-05-22',
'2018-05-23',
'2018-05-24',
'2018-05-25',
'2018-05-26',
'2018-05-27',
'2018-05-28',
'2018-05-29',
'2018-05-30',
'2018-05-31',
'2018-06-04',
'2018-06-05',
'2018-06-08',
'2018-06-09',
'2018-06-10',
'2018-06-13',
'2018-06-14',
'2018-06-15',
'2018-06-17',
'2018-06-20',
'2018-06-21',
'2018-06-24',
'2018-06-28',
'2018-06-29',
'2018-07-02',
'2018-07-05',
'2018-07-11',
'2018-07-12',
'2018-07-15',
'2018-07-16',
'2018-07-17',
'2018-07-20',
'2018-07-23',
'2018-07-24',
'2018-07-25',
'2018-07-26',
'2018-07-27',
'2018-07-29',
'2018-08-07',
'2018-08-08',
'2018-08-10',
'2018-08-11',
'2018-08-12',
'2018-08-13',
'2018-08-15',
'2018-08-17',
'2018-08-18',
'2018-08-19',
'2018-08-20',
'2018-08-21',
'2018-08-25',
'2018-08-26',
'2018-08-27',
'2018-08-28',
'2018-08-29',
'2018-08-30',
];
$changedDates = array_map(function ($date) {
return date('m/d/Y', strtotime($date));
}, $currentDates);
var_dump($changedDates);
<?php
$dates = Array
("2018-05-19", "2018-05-20", "2018-05-21", "2018-05-22","2018-05-23");
print_r($dates);
$newDateFormat = array();
foreach($dates as $date){
$date = date('m/d/Y', strtotime($date));
array_push($newDateFormat,$date);
}
print_r($newDateFormat);
?>
I Think it will help you.
<?php
function convert_YYYYMMDD_to_MMDDYYYY($str)
{
return substr($str, 5, 2) . '/' . substr($str, 8, 2) . '/' . substr($str, 0, 4);
}
for ($i =0 ; $i < count($arr) ; $i++)
{
$arr[$i] = convert_YYYYMMDD_to_MMDDYYYY($arr[$i]);
}
?>
Related
I have the following array with different mixed dates from different years
Array
(
[0] => 2016-05-18
[1] => 2016-06-18
[2] => 2016-08-13
[3] => 2016-09-03
[4] => 2016-10-08
[5] => 2016-08-06
[6] => 2016-09-30
[7] => 2016-09-10
[8] => 2016-07-09
[9] => 2016-06-13
[10] => 2016-06-15
[11] => 2016-07-30
[12] => 2016-08-27
[13] => 2016-07-02
[14] => 2016-11-01
[15] => 2016-09-18
[16] => 2016-11-06
[17] => 2016-11-07
[18] => 2017-06-17
[19] => 2017-06-22
[20] => 2017-06-21
[21] => 2017-10-01
[22] => 2017-07-08
[23] => 2017-05-27
[24] => 2017-06-06
[25] => 2017-09-09
[26] => 2017-04-16
[27] => 2017-09-16
[28] => 2017-07-29
[29] => 2017-08-05
[30] => 2017-09-03
[31] => 2017-06-24
[32] => 2017-08-26
[33] => 2017-07-22
[34] => 2018-05-28
[35] => 2018-06-09
[36] => 2017-10-16
[37] => 2017-10-28
[38] => 2017-10-08
[39] => 2017-11-04
[40] => 2018-06-20
[41] => 2018-08-05
[42] => 2018-09-03
[43] => 2018-06-16
[44] => 2018-03-31
[45] => 2019-05-25
[46] => 2021-05-25
[47] => 2021-05-26
[48] => 2021-05-27
)
I want to remove all dates earlier than 2021 so my new array will have only the dates belonging to the current year of 2021. How can I filter the new array so all dates before 2021 will disappear?
Array
(
[0] => 2021-05-25
[1] => 2021-05-26
[2] => 2021-05-27
)
You can create a function that loops through the array and check if the string stars with 2021 if not, delete the element.
For php 8 you can use function:
str_starts_with(string $haystack, string $needle): bool
For example:
foreach($array as $key => $val){
if(!str_starts_with('2021', $val)){
unset($array[$key]);
}
}
For previous php versions you can use strpos
Example:
foreach($array as $key => $val){
if (!strpos($val, '2021') == 0) {
unset($array[$key]);
}
}
You have hashtag array-filter and PHP has array_filter function:
$dates = [
'2016-05-18',
'2018-03-31',
'2019-05-25',
'2021-05-25',
'2021-05-26',
'2021-05-27',
];
function myFilter($dt) {
$year = date('Y',strtotime($dt));
return $year > 2020;
}
print_r(array_values(array_filter($dates, 'myFilter')));
You can loop over each date and use the dateTime object to compare the times to see if the date has passed a set date like the begining of a year and if it hasn't then add it to the new array. See below code:
<?php
//array with mixed date data
$dateArray = array("2021-01-01","2016-05-18","2017-09-03","2018-06-16","2019-05-25","2021-05-25","2021-05-26","2021-05-27");
//the day you want to have everyday after (in this case the start of 2021)
$setDate = "2021-01-01";
//array where the dates greater than then the set date will be stored.
$newTimesArray = array();
$setDateObj = new \DateTime($setDate); // moved outside of loop
foreach ($dateArray as $date) {
$given_date = new \DateTime($date); //convert string to a date time object
if ($given_date >= $setDateObj) { //if the date is greater than or equal to "2021-01-01" add it to the new array
$newTimesArray[]=$date;
}
}
//print the results
echo print_r($newTimesArray,true);
?>
// Listing of holidays, Down Fridays, etc...
$sqlHol = "SELECT holidayDate FROM holidays";
$holidays = array();
$stmtHol = sqlsrv_query( $conn, $sqlHol );
if( $stmtHol === false )
{
die( print_r( sqlsrv_errors(), true));
}
if ( $stmtHol )
{
$rowsHol = sqlsrv_has_rows( $stmtHol );
if ($rowsHol === false)
{
die( print_r( sqlsrv_errors(), true));
}
else while( $rowsHol = sqlsrv_fetch_array( $stmtHol, SQLSRV_FETCH_ASSOC) )
{
$holidays[] = $rowsHol['holidayDate']; // This is where I fill my array
}
}
var_dump $holidays //This is the var_dump of the array $holidays
Array ( [0] => 2018-01-01 1 => 2018-01-12 [2] => 2018-01-15 [3] => 2018-01-26 [4] => 2018-02-09 [5] => 2018-02-19 [6] => 2018-02-23 [7] => 2018-03-09 [8] => 2018-03-23 [9] => 2018-04-06 [10] => 2018-04-20 [11] => 2018-05-11 [12] => 2018-05-25 [13] => 2018-05-28 [14] => 2018-06-01 [15] => 2018-06-15 [16] => 2018-06-29 [17] => 2018-07-04 [18] => 2018-07-13 [19] => 2018-07-27 [20] => 2018-08-10 [21] => 2018-08-24 [22] => 2018-09-03 [23] => 2018-09-07 [24] => 2018-09-21 [25] => 2018-10-05 [26] => 2018-10-08 [27] => 2018-10-19 [28] => 2018-11-02 [29] => 2018-11-12 [30] => 2018-11-16 [31] => 2018-11-22 [32] => 2018-11-30 [33] => 2018-12-14 [34] => 2018-12-25 [35] => 2018-12-28 [36] => 2019-01-01 [37] => 2019-01-11 [38] => 2019-01-21 [39] => 2019-01-25 [40] => 2019-02-08 [41] => 2019-02-18 [42] => 2019-02-22 [43] => 2019-03-08 [44] => 2019-03-22 [45] => 2019-04-05 [46] => 2019-04-19 [47] => 2019-05-03 [48] => 2019-05-17 [49] => 2019-05-27 [50] => 2019-05-31 [51] => 2019-06-14 [52] => 2019-06-28 [53] => 2019-07-04 [54] => 2019-07-12 [55] => 2019-07-26 [56] => 2019-08-09 [57] => 2019-08-23 [58] => 2019-09-02 [59] => 2019-09-06 [60] => 2019-09-20 [61] => 2019-10-04 [62] => 2019-10-14 [63] => 2019-10-18 [64] => 2019-11-01 [65] => 2019-11-11 [66] => 2019-11-15 [67] => 2019-11-28 [68] => 2019-11-29 [69] => 2019-12-13 [70] => 2019-12-25 [71] => 2019-12-27 [72] => 2020-01-01 [73] => 2020-01-10 [74] => 2020-01-20 [75] => 2020-01-24 [76] => 2020-02-07 [77] => 2020-02-17 [78] => 2020-02-21 [79] => 2020-03-06 [80] => 2020-03-20 [81] => 2020-04-03 [82] => 2020-04-17 [83] => 2020-05-01 [84] => 2020-05-15 [85] => 2020-05-25 [86] => 2020-05-29 [87] => 2020-06-12 [88] => 2020-06-26 [89] => 2020-07-03 [90] => 2020-07-10 [91] => 2020-07-24 [92] => 2020-08-07 [93] => 2020-08-21 [94] => 2020-09-04 [95] => 2020-09-07 [96] => 2020-09-18 [97] => 2020-10-02 [98] => 2020-10-12 [99] => 2020-10-16 [100] => 2020-10-30 [101] => 2020-11-11 [102] => 2020-11-13 [103] => 2020-11-26 [104] => 2020-11-27 [105] => 2020-12-11 [106] => 2020-12-25 [107] => 2020-12-25 )
However, the in_array used in the count5WD does not find a match to any of the values in the array when comparing $next1WD to $holidays. An example is a student starts 4-18-2018 and should complete on 4-27-2018. It needs to exclude weekends and a down Friday (4-20-2018). The result should be 7 days from the start but, I get 8 days because the down Friday is not skipped (found).
// Calculate the number of days between start and end dates
$count5WD = 0;
$temp = strtotime( $rowsBlk['startDate'] ); //example as today is 2016-03-25
$curHrs = 59.5/9; //Total course hours/hours per day = ~7 days
while( $count5WD<$curHrs )
{
$next1WD = strtotime( '+1 weekday', $temp );
$next1WDDate = date( 'Y-m-d', $next1WD );
if( in_array( $next1WDDate, $holidays ) ) //looks for $next1WD in the array
{
$count5WD++;
}
$temp = $next1WD;
}
$next5WD = date( "m/d/Y H:i:s", $temp ); //this is the outputted future date
var_dump $next1WDDate //This is the var_dump of the array $next1WDDate
2018-04-192018-04-202018-04-232018-04-242018-04-252018-04-262018-04-272018-04-302018-05-012018-05-022018-05-032018-05-042018-05-072018-05-082018-05-092018-05-102018-05-112018-05-142018-05-152018-05-162018-05-172018-05-182018-05-212018-05-222018-05-232018-05-242018-05-252018-05-282018-05-292018-05-302018-05-312018-06-012018-06-042018-06-052018-06-062018-06-072018-06-082018-06-112018-06-122018-06-132018-06-142018-06-152018-06-182018-06-192018-06-202018-06-212018-06-222018-06-252018-06-262018-06-272018-06-28
Loading the array statically (see below), the program works just fine. I believe it has something to do with the format of the data in the array but, I cannot figure it out. Comparing the array data and the $next1WD data seems to show they are the same.
$holidays = array(
'2018-01-01',
'2018-01-12',
'2018-01-15',
'2018-01-26',
'2018-02-09',
'2018-02-19',
'2018-02-23',
'2018-03-09',
'2018-03-23',
'2018-04-06',
'2018-04-20',
'2018-05-04',
'2018-05-18',
'2018-05-28',
'2018-06-01',
'2018-06-15',
'2018-06-29',
);
I have the following array
Array ( [0] => **start** [1] => **start** [2] => name [3] => producer [4] => contact name [5] => 03354222271 [6] => fzahid001#gmail.com [7] => day contact name [8] => 03354222271 [9] => venue name [10] => adress [11] => country [12] => city [13] => desc [14] => file [15] => 2016-01-01 [16] => 01:00 [17] => 06:00 [18] => 2 [19] => music_festival [20] => 2000+ [21] => quick [22] => alcohol [23] => quick [24] => 10x10 [25] => ***no*** [26] => ***no*** [27] => ***no*** [28] => 2 [29] => 0 [30] => 4 [31] => $158 [32] => $118.5 [33] => $284 [34] => $960 [35] => **start** [36] => na [37] => producer [38] => con [39] => 1 [40] => fzahid001#gmail.com [41] => nam [42] => 1 [43] => venue [44] => ad [45] => co [46] => ci [47] => description test [48] => download555ssss.png [49] => 2016-12-07 [50] => 13:00 [51] => 19:00 [52] => 2 [53] => manual_selection [54] => ATV [55] => 10x10 [56] => no [57] => no [58] => no [59] => 1 [60] => 1 [61] => 1 [62] => $109.5 [63] => $118.5 [64] => $99.5 [65] => $728 [66] => **start** [67] => Race [68] => Race Club [69] => Faizan [70] => 03354222271 [71] => fzahid001#gmail.com [72] => Faizan Zahid [73] => 03354222271 [74] => DHA [75] => 90-H Tariq Gardens [76] => Pakistan [77] => Lahore [78] => [79] => images.jpg [80] => 2017-01-01 [81] => 00:00 [82] => 07:00 [83] => 2 [84] => cycling_road_race [85] => 1_field [86] => 1_399 [87] => electronic [88] => quick [89] => 10x10 [90] => no [91] => no [92] => no [93] => 1 [94] => 0 [95] => 1 [96] => $109.5 [97] => $118.5 [98] => $99.5 [99] => $796.5 [100] => )
Now what I want to do is, remove the consecutive duplicates of "start". I am currently using following code
foreach ($updateddata as $value){
if($value != $previousvalue){
array_push($finaldata, $value);
}
$previousvalue = $value;
}
but is also removes other consecutive duplicates as well which I don't want to remove. Kindly help me how to do this.
I have highlighted the occurrences of "start" which I want to remove and make it one single entry, while leaving the other string as it is e.g. I don't want to remove the duplicates of string "no"
Try this
if($value != $previousvalue){
array_push($finaldata, $value);
} else if( $value != 'start'){
array_push($finaldata, $value);
}
To remove the consecutive duplicates of "start" value you should restrict the crucial condition to that value. Use the following approach:
// exemplary array
$updateddata = [
'start', 'start', 'producer', 'contact', 'no', 'no'
];
foreach ($updateddata as $k => $v) {
if (isset($updateddata[$k-1]) && $v == 'start' && $v == $updateddata[$k-1]) {
unset($updateddata[$k]);
}
}
The output:
Array
(
[0] => start
[2] => producer
[3] => contact
[4] => no
[5] => no
)
I want to cut array in php. My array is listed below :
Array
(
[0] => 6/1/2014
[1] => 6/2/2014
[2] => 6/3/2014
[3] => 6/4/2014
[4] => 6/5/2014
[5] => 6/6/2014
[6] => 6/7/2014
[7] => 6/8/2014
[8] => 6/9/2014
[9] => 6/10/2014
[10] => 6/11/2014
[11] => 6/12/2014
[12] => 6/13/2014
[13] => 6/14/2014
[14] => 6/15/2014
[15] => 6/16/2014
[16] => 6/17/2014
[17] => 6/18/2014
[18] => 6/19/2014
[19] => 6/20/2014
[20] => 6/21/2014
[21] => 6/22/2014
[22] => 6/23/2014
[23] => 6/24/2014
[24] => 6/25/2014
[25] => 6/26/2014
[26] => 6/27/2014
[27] => 6/28/2014
[28] => 6/29/2014
[29] => 6/30/2014
[30] => 7/1/2014
[31] => 7/2/2014
[32] => 7/3/2014
[33] => 7/4/2014
[34] => 7/5/2014
[35] => 7/6/2014
[36] => 7/7/2014
[37] => 7/8/2014
[38] => 7/9/2014
[39] => 7/10/2014
[40] => 7/11/2014
[41] => 7/12/2014
[42] => 7/13/2014
[43] => 7/14/2014
[44] => 7/15/2014
[45] => 7/16/2014
[46] => 7/17/2014
[47] => 7/18/2014
[48] => 7/19/2014
[49] => 7/20/2014
[50] => 7/21/2014
[51] => 7/22/2014
[52] => 7/23/2014
[53] => 7/24/2014
[54] => 7/25/2014
[55] => 7/26/2014
[56] => 7/27/2014
[57] => 7/28/2014
[58] => 7/29/2014
[59] => 7/30/2014
[60] => 7/31/2014
[61] => 8/1/2014
)
In this array 0 to 29 elements if for 6th Month, 30th to 60th elements are for 7th Month etc..
Now i want this array in the below fashion
Array
(
[0] => 6/1/2014
[1] => 6/2/2014
[2] => 6/3/2014
[3] => 6/4/2014
[4] => 6/5/2014
[5] => 6/6/2014
[6] => 6/7/2014
[7] => 6/8/2014
[8] => 6/9/2014
[9] => 6/10/2014
[10] => 6/11/2014
[11] => 6/12/2014
[12] => 6/13/2014
[13] => 6/14/2014
[14] => 6/15/2014
[15] => 6/16/2014
[16] => 6/17/2014
[17] => 6/18/2014
[18] => 6/19/2014
[19] => 6/20/2014
[20] => 6/21/2014
[21] => 6/22/2014
[22] => 6/23/2014
[23] => 6/24/2014
[24] => 6/25/2014
[25] => 6/26/2014
[26] => 6/27/2014
[27] => 6/28/2014
[28] => 6/29/2014
[29] => 6/30/2014
)
Array
(
[0] => 7/1/2014
[1] => 7/2/2014
[2] => 7/3/2014
[3] => 7/4/2014
[4] => 7/5/2014
[5] => 7/6/2014
[6] => 7/7/2014
[7] => 7/8/2014
[8] => 7/9/2014
[9] => 7/10/2014
[10] => 7/11/2014
[11] => 7/12/2014
[12] => 7/13/2014
[13] => 7/14/2014
[14] => 7/15/2014
[15] => 7/16/2014
[16] => 7/17/2014
[17] => 7/18/2014
[18] => 7/19/2014
[19] => 7/20/2014
[20] => 7/21/2014
[21] => 7/22/2014
[22] => 7/23/2014
[23] => 7/24/2014
[24] => 7/25/2014
[25] => 7/26/2014
[26] => 7/27/2014
[27] => 7/28/2014
[28] => 7/29/2014
[29] => 7/30/2014
[30] => 7/31/2014
)
Array
(
[0] => 8/1/2014
)
This calculation should be in a way that if i chose other months then it also do the same process of cutting array for different months.
you can also separate array by month & and put them in main array
$arrres =array();
foreach($arr as $value)
{
$arrres[str_replace('/','',substr($value,0,2))][] = $value ;
}
print_r($arrres);
you can use this:, it will also generate arrays with same num of days in months (28,30,31)
//generate dates for test
$dates = [];
for($i = 0 ; $i < 90 ; $i++){
$dates[] = Date("d/m/Y",time()-($i*24*3600));
}
//make results, also validates that it's same month and year
$result = [];
foreach($dates as $date){
$dStr = Date("m-y",strtotime(str_replace('/', '-', $date)));
if(!isset($result[$dStr])) $result[$dStr] = [];
$result[$dStr][] = $date;
}
var_dump($result);
I'd explode the value of every index in your array and set it as an index to the new array.
$days = [];
foreach($your_array as $day) {
$days[ explode("/", $day)[0] ][] = $day;
}
var_dump( $days );
This question already has answers here:
Reverse date order in script
(6 answers)
Closed 9 years ago.
I build a php page in which i want to get dates in reverse order how i do this?
Here is my code:
$dates = array();
$timestamp = time();
for ($i = 0 ; $i <=30 ; $i++) {
$dates[$i]= date('m-d-Y', $timestamp);
$timestamp -= 24 * 3600;
}
and here is my output
Array ( [0] => 02-01-2014 [1] => 01-31-2014 [2] => 01-30-2014 [3] => 01-29-2014 [4] => 01-28-2014 [5] => 01-27-2014 [6] => 01-26-2014 [7] => 01-25-2014 [8] => 01-24-2014 [9] => 01-23-2014 [10] => 01-22-2014 [11] => 01-21-2014 [12] => 01-20-2014 [13] => 01-19-2014 [14] => 01-18-2014 [15] => 01-17-2014 [16] => 01-16-2014 [17] => 01-15-2014 [18] => 01-14-2014 [19] => 01-13-2014 [20] => 01-12-2014 [21] => 01-11-2014 [22] => 01-10-2014 [23] => 01-09-2014 [24] => 01-08-2014 [25] => 01-07-2014 [26] => 01-06-2014 [27] => 01-05-2014 [28] => 01-04-2014 [29] => 01-03-2014 [30] => 01-02-2014 )
How to store dates in reverse order?
Reverse you can use strtotime function
<?php
$dates = array();
//get the last day and go from that day
$timestamp = strtotime('-30 days');
for ($i = 0 ; $i <=30 ; $i++) {
//insert the date
$dates[$i]= date('m-d-Y', $timestamp);
//increase the day
$timestamp += 24 * 3600;
}
//display the output
print_r($dates);
Array
(
[0] => 01-02-2014
[1] => 01-03-2014
[2] => 01-04-2014
[3] => 01-05-2014
[4] => 01-06-2014
[5] => 01-07-2014
[6] => 01-08-2014
[7] => 01-09-2014
[8] => 01-10-2014
[9] => 01-11-2014
[10] => 01-12-2014
[11] => 01-13-2014
[12] => 01-14-2014
[13] => 01-15-2014
[14] => 01-16-2014
[15] => 01-17-2014
[16] => 01-18-2014
[17] => 01-19-2014
[18] => 01-20-2014
[19] => 01-21-2014
[20] => 01-22-2014
[21] => 01-23-2014
[22] => 01-24-2014
[23] => 01-25-2014
[24] => 01-26-2014
[25] => 01-27-2014
[26] => 01-28-2014
[27] => 01-29-2014
[28] => 01-30-2014
[29] => 01-31-2014
[30] => 02-01-2014
)