PHP: Previous Month from current month formula is not working - php

It might be Duplicate Question, but I tried but it doesn't work.
Something confusion is there, which I am not getting it.I tried for all months its working but its not working for "March" Month.I know it sound weird, but I tried everything.
Aim: Show Previous Month from Given Month.
PHP:
$month='March';
$pre_mth =Date("F", strtotime($month . " last month"));
echo $pre_mth;
Actual Output: March Expected Output:February
Please let me know.Thanks in advance.

Try this:
$pre_mth = date('F',strtotime('first day of last month', strtotime($month)));
echo $pre_mth;

Extra space existed before "last month". Also, specify a day to avoid conflicts.
Try this:
$month = 'March 1';
$pre_mth = Date("F", strtotime($month . "last month"));
echo $pre_mth; // February

Related

Get Week Number From Date off one week

Trying to get the week number from a given date within PHP. For some reason various things I try seem to be a week off.
Looking at the following link we should be on week 3
http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php
Yet when I do the following I get week 2
echo "Weeknummer: " . date("W", strtotime("2016-01-17"));
I've also tried this code getting the same result, week 2
$date = new DateTime("2016-01-17");
$week = $date->format("W");
echo "Weeknummer: $week";
Any ideas why its seems to be a week behind and how I can fix that?
Thanks
We are, but today is the 19th (at least in my time-zone...).
You have hard-coded the 17th in your script and as you can see in the site that you mentioned, that is the last day of week 2.
The link you sent shows that 2016-01-17 is on week 2. See screenshot. In PHP the weeks start on Monday (you can read more in the docs)
It's because in mktime(), it goes like this:
mktime(hour, minute, second, month, day, year);
so try with the below code hope it's what you want
<?php
$ddate = "2016-01-17";
$duedt = explode("-", $ddate);
$date = mktime(0, 0, 0, $duedt[1], $duedt[2], $duedt[0]);
$week = (int)date('W', $date);
echo "Weeknummer: " . $week;
?>
also you can try github.com/briannesbitt/Carbon to maximize your skills in Date manipulation. For carbon you can do that by:
Carbon::createFromFormat('Y-m-d H', '2012-10-18')->format('W');

Using the PHP date and time function to print a certain sentence

I'm in need of help with the PHP date/time function.
Using de date/time function, I need to print the following:
Today is 'current day', the 'number of day of month', in the year 'year'. Today is 'number of day of the year'. We still have 'number of days till end of year'.
Just can't get this working.. any ideas?
Thanks!
Alright so there may be a better way to do this, but this is one way of getting all the different elements you asked for:
$d = new DateTime();
$currentDay = $d->format('l'); // Will give you the current day in full (i.e. Monday)
$dayOfMonth = $d->format('j'); // Will give you the current day of the Month
$year = $d->format('Y'); // Will give you the current Year.
$dayOfYear = ($d->format('z') + 1); // Starts at 0, so we add one to get the actual days past.
As for the days left, I reckon there is potentially a better way to do this, but here's a quick solution:
$daysInYear = 365;
if ($d->format('L') === 1) { // Returns 1 if leap year.
$daysInYear = 366;
}
$daysLeft = $daysInYear - $dayOfYear;
That should give you all the variables you need to print out the statement you asked for.

$date format("M y") always showing the wrong month (the one before)

I got this code:
list($year, $month, $day) = explode("-", $row['game_release']);
if (checkdate($month, $day, $year)) {
$date = (new DateTime($row['game_release']))->format("j M 'y");
} else {
$date = (new DateTime($row['game_release']))->format("M 'y");
}
echo"
<tr>
<td> ".$date." </td>
The reason why I need it, is because some dates are invalid 00-05-2015 for example. I use this to list games as announced, but without a confirmed release date. I realize they are invalid, but if I query for dates BETWEEN 2015-04-00 AND 2015-04-30 they show up under April like they should.
However, in the code above, which I'm using in my search engine, a game listed as 2015-04-00 would appear as 'March '15'. Why is this happening and how can I solve it?
Why is this happening
Because months usually start on day 1, thus day 0 is the previous month.
and how can I solve it?
else {
$date = (new DateTime(($day+1) . "$month-$year"))->format("M 'y");
}
As Marco Bernardini suggested add one day to the not released date and show the correct month.
If it were me. I would not rely on empty date alone. I would add a flag_release to the database table which I would verify as 0 everytime I show the upcoming releases.
$date = (new DateTime($row['game_release']));
$date->modify("+1 day");
echo $date->format("M 'y");

Get day most closely following today from list of days in PHP string

I need to look at the contents of a string and determine what the day most closely following today would be.
For example. lets say I have a string called $available_day_list with the value "monday, thursday, friday, saturday,."
According to the list above, If today was tuesday, I would like to display "thursday."
If today was saturday, I would like to display "tuesday."
I am getting the value of today with:
$current_day = date("l");
$current_day = strtolower($current_day);
Anyone know how I might be able to do this without an array? Thanks all!!
Try this:
$days = explode(',',$available_day_list);
$closestDay = '';
$minTime = 620000;// more than a week
foreach($days as $day){
$diff = strtotime('next '.$day) - time();
if($diff < $minTime){
$closestDay = $day;
$minTime = $diff;
}
}
echo $closestDay;
(i know, should've been written as a comment, but can't yet) Yotam's code turns the string into an array. And it works great, I'm not sure why you don't want to use arrays.
If you really don't though, you'll basically have to determine the current day, and then search through the string for the next day, and if not the next, and so on, until you find one.

Calculate date php issue with strtotime

Still trying to figure out date with PHP. I have this:
$fri_end_date = date('Y-m-d',strtotime('Friday'));
$fri_start_date = date('Y-m-d ',strtotime('Friday'));
$put = $fri_start_date.$fri_end_date;
echo "$put";
I want to calculate the date for each Friday but display that Friday date through till Sunday. Then on Monday it would show the next coming Friday. Basically want to show the same Friday date throughout the weekend. How would I do this?
I did understand your question now and I think this will solve your issue:
if( date('w') >= 6 )
$fri_end_date = date('Y-m-d',strtotime('last Friday'));
else
$fri_start_date = date('Y-m-d ',strtotime('Friday'));
$put = $fri_start_date.$fri_end_date;
echo "$put";
Using date('w') will return you the day of the week, that way you can test if it is saturday or sunday and use the 'last Friday' or else use the "current" Friday.
Cheers,
Denis
Try this and let me know if it is what you want:
$fri_end_date = date('Y-m-d',strtotime('last Friday'));
$fri_start_date = date('Y-m-d ',strtotime('next Friday'));
$put = $fri_start_date.$fri_end_date;
echo "$put";
This will display the date of the last Friday before the day you run the code and the next Friday.
Cheers,
Denis
The problem is that you are not really clear about your problem. That sayd, if I understand correctly you need to display 2 days before and after the current Friday. If that is the case try the code below.
$fri_end_date = date('Y-m-d',strtotime('Friday -2 days'));
$fri_start_date = date('Y-m-d ',strtotime('Friday +2days'));
$put = $fri_start_date.$fri_end_date;
echo "$put";
If it is not, try to be more concise and maybe throw in an example or two ;)
Cheers,
Denis

Categories