Substracting the n value in date() - php

I want to implement a Javascript countdown timer that has the month value substracted by 1.
To get the date dynamically via PHP I use this code:
$date = "2014:3:19 00:00:00";
$newDate = date("Y, n, j, H, i", strtotime($date));
Which returns:
2014, 3, 9, 00
My question is how can I substract the value n by 1, so the final output will be always like this:
2014, (3-1), 9, 00

Here's the DateTime() way to do it (I used dashes instead of colons as that is the proper separator for date parts):
$date = "2014-3-19 00:00:00";
$date = (new DateTime($date))->modify('-1 month')->format("Y, n, j, H, i");
or
$date = "2014-3-19 00:00:00";
$date = (new DateTime($date))->diff(new DateInterval('P1M'))->format("Y, n, j, H, i");

If you mean minus one month, then you could do:
$date = "2014-3-19 00:00:00";
$newDate = date("Y, n, j, H, i", strtotime('-1 month', strtotime($date)));
And 2014, 1, 19, 00 will be 2013, 12, 19, 00 but not 2014, 0, 19, 00.
Update:
You want to pass a date to the jQuery plugin(jquery.magicbusmultimedia.net).
The plugin only ask you to pass a javascript Date object.
So you could do:
$('#myCounter').mbComingsoon(new Date(<?php echo strtotime($date); ?> * 1000));

$date = "2014-3-19 00:00:00";
$newDate = date("Y, n, j, H, i", strtotime('-1 month', strtotime($date)));
And 2014, 1, 19, 00 will be 2013, 12, 19, 00 but not 2014, 0, 19, 00.

Related

Parsing Datetime in PHP

I have the following date forma string: January 5, 2016 07:22am ET
I would like to be able to parse such date format using DateTime::createFromFormat. I have tried with the following and no success:
$new_date = DateTime::createFromFormat($date, 'D d, Y G:i e');
where $date is the string January 5, 2016 07:22am ET
Any idea?
Sets the time zone for the DateTime object
$format = 'Y-m-d';
$date = DateTime::createFromFormat($format, '2016-05-27');
$new_date=$date->format('D d, Y G:i e') ;
echo $new_date;
And Your Output Will Be
Fri 27, 2016 12:51 UTC
Example:
$date = new DateTime(
'January 5, 2016 07:22am', //your time string
new DateTimeZone('UTC') //set you timezone
);
echo $date->format('F d, Y h:ia e');
will output
January 05, 2016 07:22am E
The ET is incorrect Timezone word for PHP.
Without that You can create DateTime like this:
$date = new DateTime('January 5, 2016 07:22am');

Very specific date formatting in PHP

I'm trying to import a date "9:26 AM Thursday Feb 11, 2016" but it only shows "12:00 AM Thursday Feb 11, 2016"
$date = date("h:i A l M j, Y", strtotime("9:26 AM Thursday Feb 11, 2016"));echo $date;
If you use the incredibly flexible DatTime class it is quite easy.
And using the ->format() method once you have a valid date loaded you can output it in any format you like
$d = DateTime::createFromFormat('h:i A l M j, Y', "09:26 AM Thursday Feb 11, 2016");
echo $d->format('h:i A l M j, Y');
echo $d->format('d/m/Y H:i');
echo $d->format('d/m/Y H:i l');
Gives this
09:26 AM Thursday Feb 11, 2016
11/02/2016 09:26
11/02/2016 09:26 Thursday

PHP- strtotime not converting properly

I don't understand why strtotime is returning a date off by a few days.
$expiration = '2015-07-19'; // yyyy-mm-dd
$exp = strtotime($expiration);
$exp = date('F m, Y',$exp);
echo $exp; // returns "July 07, 2015" (NOT the 19th)
What am I missing?
UPDATE: Even if I do this:
echo date('F m, Y');
It says it's August 8 when today is August 21! Why!?!?!?
Here it is: Outputs July 19, 2015. See demo. You are using two characters to represent months in your date function. F and m are textual and numeric representations of a month. So change 'm' to 'd' to represent days. date("F d, Y") e.g. January 01, 2000
$expiration = '2015-07-19'; // yyyy-mm-dd
$exp = strtotime($expiration);
$exp = date('F d, Y',$exp);
echo $exp;
DEMO
Try this
$expiration = '2015-07-19'; // yyyy-mm-dd
$exp = strtotime($expiration);
//$exp = date('F m, Y',$exp);
$exp = date('jS F, Y',$exp);
Output
19th July, 2015

Convert date integer to text?

Is there anyway to convert the value of idate("z") to a date format that reads the Day, Month, and Year? My code looks like this:
$date_int = idate("z");
$date_text = strtotime($date_int);
$date = date("l, F j, Y", $date_text);
For some reason, it's still echoing Thursday, January 1, 1970.
Any ideas?
idate("z") is incorrect as that will return the day of the year. It seems like you want idate("U"), but in that case just use date() without the second parameter, it will assume time(). Example:
$date = date("l, F j, Y");
That should be all you need.
idate("z") will only return the day of the year. Today being 74. strtotime will not understand 74 as a parsing format. Therefore date() fails all together.
Assuming you want the date of the CURRENT YEAR:
$dayOfYear = 80;
print date("l, F j, Y", strtotime($dayOfYear - idate("z") . " day")); // Wednesday, March 21, 2012
$dayOfYear = 1;
print date("l, F j, Y", strtotime($dayOfYear - idate("z") . " day")) // Monday, January 2, 2012

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