Is there any way to get the full date from a string that does not actually contain the month?
i am given the date in a format of Wednesday 16th
and need to add this to my database with a month,
The application i am making is for lifestyle couriers and they get their manifests in that format and have the last 2 months available, so i need to find out the month?
<?php
$date = 16;
$day = "Wednesday";
$oneMonthAgo = explode(" ",date("m Y F",strtotime("-1 month")));
$twoMonthsAgo = explode(" ",date("m Y F",strtotime("-2 months")));
if (date("l",strtotime($date."-".$oneMonthAgo[0]."-".$oneMonthAgo[1]))==$day) {
echo "Last month: ".$oneMonthAgo[2];
}
else if (date("l",strtotime($date."-".$twoMonthsAgo[0]."-".$twoMonthsAgo[1]))==$day) {
echo "Month before last: ".$twoMonthsAgo[2];
}
else {
echo "Not valid";
}
?>
Because you are using PHP, there is an easy way to do this. In other languages, you would have to use a weekday algorithm.
In PHP, we can convert a properly formatted string into a time, from which we can then derive date information. Examples of a properly formatted string is -1 month
or -2 months
, which return time objects for the previous month or the month before that, respectively.
Using the date()
function, we can get the month, year and textual month values for those months. We delimiter these by a space, so that we can explode these values into an array after they are found. Thus, we now have two arrays, $oneMonthAgo
and $twoMonthsAgo
, which both contain their month's respective number, year and textual month at the 0
, 1
and 2
indexes respectively.
Now, we can create another time, by appending the date that we are looking for ($date
) onto these values, with the proper delimiters in-between (when using -
as the delimiters, PHP assumes a d-m-Y
format, when using /
PHP assumes a m-d-Y
format). Then, by passing that through another date()
function, we can find the data that we are looking for: the textual day that took place at that date. Now we can simply compare that to the day that we are looking for, $day
. If the textual day is the same, then this is the month which we are looking for, so we can print out the full textual month.
We do this in an if
/ else
statement for both months, and if we still haven't found it, assume that the date is invalid.
date()
function, strtotime()
function, Valid dates for strtotime()
Yes
and no
. Since full date
requires month to be known you need to know what to do with lack of that information. If you "guess" the month (be it current one or random, does not matter), then "yes" is close. If you cannot tell what the month we talk about then answer is no
(or random
).
PS: you may need a year
too...
Assuming that you want to regard the current and the last two months, I can imagine, that there will be no two day numbers of the same week day in that timespan (that should be proven first).
I would iterate back over the last n days using PHP's date function and try to detect your "Wednesday 16th" where n is the aggregate of month days that date with param "t" returns for the current and the last two months.
If you have your match then, you know the month and the year (the year could be the previous year as well if you start in January or February).
I use a while loop to loop backwards however many months needed to find what is searched for.
$date = 16;
$day = "Wednesday";
$x=0;
$start = date("Y-m-") . $date;
While(date("l", strtotime($start ."-" .$x . "months")) != $day){
$x++;
}
Echo $x . " months ago.";
// Or to output the date:
//Echo date("Y-m-d", strtotime($start ."-" .$x . "months"));
This will output 0 months ago.
But if input $date= 15; it will output 5 months ago (March 2017).