How to show yesterdays date in this format? - php

I know there is a lot of info on the neton how to show yesterdays date but I am unable to get any of them to work in this format
How do you show yesterdays date if todays date is in this format date('F jS, Y') ?
July 27th, 2009 should show July 26th, 2009
//Does not work
$yesterday = date('F jS, Y', mktime(0, 0, 0, date("F") , date("j") - 1, date("Y")));
echo $yesterday;

Use the very awesome strtotime:
$today = 'July 27th, 2009';
$yesterday = date('F jS, Y', strtotime('yesterday', strtotime($today)));
print $yesterday; // July 26th, 2009

easier:
$yesterday = date('F jS, Y', time()-86400);

Related

Why is date() returning the wrong month?

date('F Y', strtotime('2013-05-00T00:00:00')); returns the value 'April 2013', but I would expect it to return 'May 2013'.
Is this an issue with date() interpreting the date string as still in April, or perhaps the format string? I tried 'M Y', which still gave me 'Apr 2013'. Is there an alternative?
date works on a timestamp not a textual representation of a date
If you change the 0 to a 1 for day of month (thus getting a valid date to start from) and use strtotime
date('F Y', strtotime('2013-05-01T00:00:00'));
Then it will work
PHP interprets day of the month 0 as the last day of the previous month.
Valid values for day of the month are 01-31
echo date('d F Y', mktime(0, 0, 0, 5, 0, 2013)); //30 April 2013
echo date('d F Y', mktime(0, 0, 0, 5, 1, 2013)); // 01 May 2013
date('F Y', strtotime('2013-05-00T00:00:00'));
0 is not a valid start date. You have to assign it to 1. Then it will work as you expected.
date('F Y', strtotime('2013-05-01T00:00:00'));

Convert yyyy-mm-dd hh:mm:ss to dayoftheweek, dd of monthname yyyy

I almost have this by
$this->fecha = '2013 05-05 05:55:05'
$yearvalue = date("Y", strtotime($this->fecha) );
$monthname = date("F", strtotime($this->fecha) );
$dayvalue = date("d", strtotime($this->fecha) );
$printFecha = "$dayvalue of $monthname $yearvalue";
Which outputs 5 of May 2013
How can I get the name of the day of the week?
You can do it like this,
echo date('l j \of F Y', $this->date);
Your output will be something like this
Sunday 5 of May 2013
Using date of 2005-05-12:
echo date("l", mktime(0, 0, 0, 5, 12, 2005));
you'd get 'Thursday'.

Date Format then add 3 hours

How could I take something that is formatted such as June 15, 2012 06:37PM in the PST timezone and convert it to 06/15/12 06:37PM but in the EST timezone (thus it will be 06/15/12 09:37PM).
If you use PHP >= 5.2.0 you can try this solution:
$date = 'June 15, 2012 06:37PM';
$nDate = DateTime::createFromFormat('F d, Y h:iA' , $date, new DateTimeZone('PST'));
$nDate->setTimezone(new DateTimeZone('EST'));
echo $nDate->format('m/d/y h:iA');
Without using timezones you could just do:
$new_date = strtotime($date) + strtotime("+3 hours");
$new_date = date('m/d/y h:ia', $new_date);

What happening to php date function [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP date() and strtotime() return wrong months on 31st
I have this code and it outputs something strange i think. So, what i am doing wrong here.
<?php
$sP1 = date('m Y');
$sP2 = date('m Y', strtotime('+01 month'));
$sP3 = date('m Y', strtotime('+02 month'));
$sP4 = date('m Y', strtotime('+03 month'));
echo $sP1.'<br>';
echo $sP2.'<br>';
echo $sP3.'<br>';
echo $sP4.'<br>';
?>
and this outputs
05 2012
07 2012
07 2012
08 2012
i think the second one should be
06 2012
Anybody know any solution?
Today is the 31st next month only has 30 days so it would be 7/12 in 1 month from today
assuming that today is May 31 2012
date('m Y') == 05 2012
date('m Y', strtotime('+1 month')) == 07 2012 because june has 30 days
date('m Y', strtotime('+2 month')) == 07 2012
date('m Y', strtotime('+3 month')) == 08 2012
date('m Y', strtotime('+4 month')) == 10 2012
I would take today's date and find the first day of the month then add a month to that if you are doing something that needs to get each month
As others have said, it is because today is the 31st and +1 month equals June-31 which changes to Jul-1. If you include the day in the date string, you can see exactly this.
<?php
$sP1 = date('m-d-Y');
$sP2 = date('m-d-Y', strtotime('+01 month'));
$sP3 = date('m-d-Y', strtotime('+02 month'));
$sP4 = date('m-d-Y', strtotime('+03 month'));
echo $sP1."\n";
echo $sP2."\n";
echo $sP3."\n";
echo $sP4."\n";
/* Outputs:
05-31-2012
07-01-2012
07-31-2012
08-31-2012
*/
?>
strtotime though can take the start date as part of the string so as King suggested, calculate the +N months from the first. So a string like May-1-2012 +01 month such as:
<?php
$sP1 = date('m Y');
$sP2 = date('m Y', strtotime(date('M-1-Y').' +01 month'));
$sP3 = date('m Y', strtotime(date('M-1-Y').' +02 month'));
$sP4 = date('m Y', strtotime(date('M-1-Y').' +03 month'));
echo $sP1."\n";
echo $sP2."\n";
echo $sP3."\n";
echo $sP4."\n";
/* Outputs:
05 2012
06 2012
07 2012
08 2012
*/
?>
http://codepad.org/auYLHvDI
It is working as intended. In a nutshell, it is because what is "one month" from May 31? June 30? August 1?
My suggestion is that if you need sequential months, calculate the offset from the start of the current month, not the current day. Or compose the date that you're looking for manually using the month, day, and year parts broken up.

PHP: simplest way to get the date of the month 6 months prior on the first?

So if today was April 12, 2010
it should return October 1, 2009
Some possible solutions I've googled seem overly complex, any suggestions?
Hm, maybe something like this;
echo date("F 1, Y", strtotime("-6 months"));
EDIT;
if you would like to specify a custom date use;
echo date("F, 1 Y", strtotime("-6 months", strtotime("Feb 2, 2010")));
A bit hackish but works:
<?php
$date = new DateTime("-6 months");
$date->modify("-" . ($date->format('j')-1) . " days");
echo $date->format('j, F Y');
?>
use a combination of mktime and date:
$date_half_a_year_ago = mktime(0, 0, 0, date('n')-6, 1, date('y'))
to make the new date relative to a given date and not today, call date with a second parameter
$given_timestamp = getSomeDate();
$date_half_a_year_ago = mktime(0, 0, 0, date('n', $given_timestamp)-6, 1, date('y', $given_timestamp))
to output it formatted, simply use date again:
echo date('F j, Y', $date_half_a_year_ago);
It was discussed in comments but the accepted answer has some unneeded strtotime() calls. Can be simplified to:
date("F 1, Y", strtotime("Feb 2, 2010 - 6 months"));
Also, you can use DateTime() like this which I think is equally as readable:
(new DateTime('Feb 2, 2010'))->modify('-6 months')->format('M 1, Y');
Or using static method....
DateTime::createFromFormat('M j, Y','Feb 2, 2010')
->modify('-6 months')
->format('M 1, Y');

Categories