Reverse date order in script - php

I have a recruitment script written in PHP that I am working with at present. On our registrations page there is a drop down for date of birth which you can see in action here:
http://www.v3recruitment.com/users/register.php
As you can see, the date starts at 1900, what I would like to do is reverse the date order. I think I have found the code thats generating the dates, but my PHP is not good enough to work out how to get it to show dates in a reverse order, here is that code:
function FetchYears($type){
$start = 1901;
$end = date("Y");
for($i=$start;$i<=$end;$i++){
$array[]['year'] = $i;
}
return $array;
}
The front end is using Smarty Templates and here is that code:
{section name="c" loop=$dob_years}
<option value="{$dob_years[c].year}" {get_value field=select selectvalue=$dob_years[c].year fieldname="dob_year"}>{$dob_years[c].year}</option>
{/section}
Can anyone please help me with this?

Either reverse loop or use array_reverse()
change return$array; to return array_reverse($array);
For smarty it's very simple you don't have to even create this function in PHP it can be done in smarty as well.
{foreach $dob_years as $entry)
<option value="{$entry.year}">{$entry.year}</option>
{/foreach}
Check HTML_SELECT_DATE in smarty or simply use just the code below doesn't require any additional php code.
{assign var=currentYear value=$smarty.now|date_format:"%Y"}
{assign var=fromYear value="2003"}
{section name=years start=$currentYear step=-1 loop=$fromYear}
{$smarty.section.years.index}
{/section

array_reverse does what it says on the tin.

Try like this
function FetchYears($type){
$start = date("Y");
$end = 1901;
for($i=$start;$i>=$end;$i--){
$array[]['year'] = $i;
}
return $array;
}
or simply use array_reverse while returning your array like
function FetchYears($type){
$start = 1901;
$end = date("Y");
for($i=$start;$i<=$end;$i++){
$array[]['year'] = $i;
}
return array_reverse($array);
}

function FetchYears($type){
$start = date("Y");
$end = 1901;
for($i=$start;$i>=$end;$i--){
$array[]['year'] = $i;
}
return $array;
}

for ($i = $end; $i >= $start; $i--) {
$array[]['year'] = $i;
}
Haven't tested it yet, should work :)

Probably the easiest way to do this would be to put the dates in UTC (integer) format.
$dates[$i] = $timestamp;
Then use the PHP sort function to sort the dates.
sort($dates,SORT_NUMERIC);
You can use rsort for the opposite.
rsort($dates,SORT_NUMERIC);
When you are finished, iterate through your array and covert back using the date() function.
echo (date('m-d-Y', $dates[$i]));

Related

How to remove values from an array in Laravel

I am trying to make a really basic day booking system and need to return all dates within a range, and then remove selected dates from that range. I tried the following code but realised that will remove duplicates which is fine, but I also need that date to be removed too.
Can anyone suggest a good way of doing this?
In the below example I am just hoping to see:
2022-04-03T00:00:00.000000Z
2022-04-04T00:00:00.000000Z
2022-04-05T00:00:00.000000Z
$start_date = "2022-04-01";
$end_date = "2022-04-05";
$datesToRemove = [
'2022-04-01T00:00:00.000000Z',
'2022-04-02T00:00:00.000000Z'
];
$range = Carbon::parse($start_date)->toPeriod($end_date)->toArray();
$available = array_unique(array_merge($range, $datesToRemove));
return $available;
To compare it is necessary to have the compared values in the same format. I decide to morph the $datesToRemove to Carbon format. The you can use to nested loops and check with PHP in_array() function.
$start_date = "2022-04-01";
$end_date = "2022-04-05";
$datesToRemove = [
"2022-04-01T00:00:00.000000Z",
"2022-04-02T00:00:00.000000Z"
];
$range = \Carbon\Carbon::parse($start_date)->toPeriod($end_date)->toArray();
$datesToRemove2 = [];
foreach($datesToRemove as $r) {
$datesToRemove2[] = \Carbon\Carbon::parse($r);
}
$res = [];
foreach($datesToRemove2 as $index => $d1) {
if(in_array($d1, $range)) {
unset($range[$index]);
}
}
return $range;
output
{
"2":"2022-04-03T00:00:00.000000Z",
"3":"2022-04-04T00:00:00.000000Z",
"4":"2022-04-05T00:00:00.000000Z"
}
Means that

Compare two array using laravel

Hi everyone I have an array
$dates = [2016-11-02,2016-11-05,2016-11-11,2016-11-15,2016-11-16]
in this I am getting all dates now I want to compare with $currentdate (2016-12-16) like this
if($dates <= $currentdate){
echo "true";
} else{
echo "false";
}
Any help will be much appreciated.
Not sure about other answers, but if you are trying to validate all the dates, please use the below code... Should work :)
$isLess = false;
$currentDate = Carbon::now();
foreach($dates as $data) {
if(Carbon::parse($date)->lte(currentDate)){
$isLess = true;
break;
}
}
echo $isLess;
Let me know in the comments if you face any problem :)
you can parse it to Carbon and use its comparison function
$date = Carbon::parse($date);
if($date->lte(Carbon::today())){
// your code
}
Here
lte() means less than equal to
to know more about Comparison operator of carbon visit http://carbon.nesbot.com/docs/#api-comparison
Since you're using Laravel, you can do that with Carbon:
if (Carbon::now()->gte(Carbon::parse($oneDate))
In one line (albeit not the prettiest)
if (collect(collect($dates)->filter(function($date) use($currentDate) { return Carbon::parse($date)->lte($currentDate); }))->count > 0) {
}

nearest date in php strtotime

I have several date(strtotime) in a Variable and want the first nearest date that is after the specified date(my date) with php. what do i do?
Variable:
$varD = "1481691600,1482642000,1482037200";
my date:
1481778000 => (2016-12-15)
several date(strtotime):
1481691600 => (2016-12-14)
1482642000 => (2016-12-25)
1482037200 => (2016-12-18) //result
result:
1482037200 => (2016-12-18)
$varD = "1481691600,1482037200,1482642000";
$myDate = "1481778000";
After you explode the string of timestamps ($varD), you can filter them and return the minimum value of the result. Here is one way to do that using array_filter and min.
$comp = function($x) use ($myDate) { return $x > $myDate; };
$firstDateAfterYours = min(array_filter(explode(',', $varD), $comp));
But if you already know that the timestamps in the string will be in ascending order, it will probably be faster not to convert the whole thing to an array and sort through it. You can use strtok to go through it piece by piece and just stop as soon as you get to a timestamp larger than your target.
$ts = strtok($varD, ',');
while ($ts !== false) {
$ts = strtok(',');
if ($ts > $myDate) break;
}
$firstDateAfterYours = $ts;

usort Date Assistance

I am trying, unsuccessfully, to sort my list by one of my template fields ($fields[3]) which is a text input field with a date inside it. The problem is that because of the format of the date it will not sort correctly because I guess it is just comparing numbers.
I think I need to change the dateformat before sorting occurs but have so far failed to do this.
I am trying to use the usort function like so (field 3 is the one with the date in which is formatted like this dd/mm/yyyy),
if (!function_exists('do_sort')) {
function do_sort($a, $b) {
return $a->fields[3] > $b->fields[3];
}}
$data = $params['data'];
usort($data, 'do_sort');
$smarty->assign('sorted', $data);
This sorts the data but as before, because I haven't changed anything, not in the correct order. Can anyone help me reformat the date prior to sorting?
Many thanks
Chris
Managed to do it. If anyone else is interested I used the following function:
function date_compare($a, $b)
{
$t1 = strtotime($a->fields[3]);
$t2 = strtotime($b->fields[3]);
return $t1 - $t2;
}
$data = $params['data'];
usort($data, 'date_compare');
$smarty->assign('sorted', $data);
Try converting the strings to timestamps first:
function do_sort($a, $b) {
$aval = strtotime($a);
$bval = strtotime($b);
if ($aval == $bval) {
return 0;
}
return $aval < $bval ? -1 : 1;
}

How do I get max(); to display the highest value number rather than just "Array"?

max($caption);
gives me this:
Array
$caption is defined here:
$i = "0";
while ($i < $count) {
list($oldCaption, $year, $order) = explode("-", $galleryDirectory[$i]);
$caption[$year][$order] = str_replace("_", " ", "$oldCaption");
echo $year; //debug
echo "<br />";
$i++;
}
$year comes out to be
2008
2009
2009
so how do I get max($caption); to give me the value of 2009 rather than Array?
also i would put the whole code, but when i tried that it turned out messy. but I will try again, so you guys can see the whole pictures
Use array_keys():
$years = array_keys($caption);
$maxYear = max($years);
// or the one-liner:
$maxYear = max(array_keys($caption));
The reason why your code wasn't working is that you were comparing the values of the array $caption, which is an another array. max() compares int values, not keys nor arrays. By using array_keys() on $caption creates another array with all years as values.

Categories