I just hope this question won't be marked as a duplicate because I've seen similar questions on stackoverflow but they all talk about adding days to the date, The problem here is that i want to add some particular months to a particular date which is gotten from my database I've tried adding it using strtotime() but the date just returns 1st January 1970, the code looks like this
<?php echo date('jS F Y', strtotime("$date +1 month")); ?>
//This is the value of date
$date = $student->date;
How to I add months to this particular date? Please note that the date is a timestamp in my database.Thanks
You have a Unix timestamp, not an actual date. Here I use the DateTime class to create a datetime object using that Unix timestamp. Then I can add a month to it and format the output.
$date = new DateTime('#'.$student->date);
$date->modify('+1 month');
echo $date-format('jS F Y');
If you want to stick to using date() and strtotime() you would use this:
echo date("jS F Y", strtotime("+1 month", $student->date));
strtotime() would take the starting date as the second parameter and then how you wish to modify it as your first parameter.
You should check out the documentation here,
But the just of it is the $date->add function. It allows you to add any amount of time to a timestamp using a DateInterval. Its a little tricky to get used to but here are a couple of examples:
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s') . "\n";
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P7Y5M4DT4H3M2S'));
echo $date->format('Y-m-d H:i:s') . "\n";
?>
which outputs:
2000-01-01 10:00:30
2007-06-05 04:03:02
The date interval is formatted in years months days hours minuets seconds, simply put in the amount you want and it will add it, so in your case:
<?php
$date->add(new DateInterval('P1M'));
echo $date->format('Y-m-d H:i:s') . "\n";
?>
PHP's strtotime() function allows for a second parameter that allows you to set a relative date.
If you would like to add a month to tomorrow, here's how:
<?php
echo date("jS F Y", strtotime("+1 month", strtotime("2014-10-09")));
// returns: 9th November 2014
Related
Is there a way of calculating the EXACT date after/before 6 months in Php taking into consideration the dynamic change in the days of the month i.e. consider some months have 30 days and some have 31 and 28 of course. I know it can be done with MySQL but I want to know if there is an option with Php as well.
Thanks in advance.
you need to date_create, date_interval_create_from_date_string and date_format to complete the job.
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('6 months'));
echo date_format($date, 'Y-m-d'); // 2000-07-01
With Object Oriented:
Adding
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P6M'));
echo $date->format('Y-m-d') . "\n"; // 2000-07-01
Subtracting
$date = new DateTime('2000-01-01');
$date->sub(new DateInterval('P6M'));
echo $date->format('Y-m-d') . "\n"; // 1999-07-01
get date before 6 month from current month:
echo date("F 1, Y", strtotime("-6 months"));
Can anybody tell me why strtotime() seems to be adding 1 day? This seems to only happen in the late afternoon (something like 7 or 8 PM), otherwise it says the correct day.
echo date('m/d/Y h:i:s a', time());
Output:
12/21/2015 08:34:43 pm
echo gmdate('l, F jS, Y', strtotime(date('m/d/Y h:i:s a', time())));
Output:
Tuesday, December 22nd, 2015
I would like the above output, however, I want today's date (the 21st not the 22nd).
Use date instead of gmdate.
You are using gmdate() which gets the date in UTC. The problem only happens late in the afternoon/evening because at those times it really is the next day in UTC time.
You're also doing too much work - you can simplify that line of code to this:
// echo gmdate('l, F jS, Y', strtotime(date('m/d/Y h:i:s a', time())));
echo date('l, F jS, Y');
Otherwise you've created a timestamp from a time string based on the current time stamp. You could just leave the second parameter to date empty and the current time "now" is assumed.
It is also very important to make sure you are calling date_default_timezone_set somewhere or that you have it configured in your php.ini.
This detail in your code...
echo gmdate('l, F jS, Y', strtotime(date('m/d/Y h:i:s a', time())));
(= the "gmdate") will always return Greenwich Mean Time (GMT), which is London/UK.
So change that to date(....
And add date_default_timezone_set('America/New_York'); anyway...
Decided to ultimately use:
$date = new DateTime(date('Y-m-d'), new DateTimeZone('America/New_York'));
$timestamp = $date->format('U');
$date = gmdate('l, F jS, Y', $timestamp);
based on Alexander's comment.
So, I have the following php:
<?php echo get_post_meta($post->ID,'dfd_ModificationTimestamp',true); ?>
It outputs dates as the following: 13/06/2015 11:02:18
I am not sure how to change it to "3 days ago" or "2 month ago" etc.
Any suggestions?
Thanks!
I think you are looking for the wordpress function human_time_diff
Example:
//Get your date
$date = get_post_meta($post->ID,'dfd_ModificationTimestamp',true);
//Convert it to a unix time stamp
$timestamp = strtotime($date);
//Print a nice string showing how long ago that was
echo human_time_diff( $timestamp, current_time('timestamp') ) . ' ago';
Use this:
$dateString = get_post_meta($post->ID,'dfd_ModificationTimestamp',true);
/* #var \DateTime $date */
$date = DateTime::createFromFormat('d/m/Y H:i:s', $dateString);
Now $date is DateTime object. You can modify it as you wish.
For example, you need 3 days ago from your date value(13/06/2015 11:02:18)
$date->modify('-3 days'); // $date becomes 10/06/2015 11:02:18
or if you need 2 months ago from your date value(13/06/2015 11:02:18)
$date->modify('-2 months'); // $date becomes 13/04/2015 11:02:18
To print $date value after modify, use:
echo $date->format('d/m/Y H:i:s');
See http://php.net/manual/en/class.datetime.php for more custom information.
I know you tagged this PHP but you can do this with javascript using the timeago plugin.
<abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr>
is converted into
<abbr class="timeago" title="July 17, 2008">7 years ago</abbr>
You need to format the time in ISO_8601 format too, something like this would do it:
echo (new DateTime('17 Oct 2008'))->format('c');
Sources:
http://timeago.yarp.com/
How to display a date as iso 8601 format with PHP
I want to always echo out the date 3 days from now. So right now I have:
$date = date("l F jS");
echo $date;
Which echos "Friday June 5th"
What exactly do I do so that it echos out "Monday June 8th" and tomorrow it echos out "Tuesday June 9th" (always 3 days ahead).
You can use strtotime() with a relative date format. When you pass a Unix timestamp as the second parameter to date() it will format that date.
$date = date("l F jS", strtotime('+3 days'));
echo $date;
Or if you prefer OOP use DateTime(). With DateTime() you can put the relative date format right into its constructor. It also handles things like daylight savings time which may come into play depending on what you're doing.
$date = new DateTime('+3 days');
echo $date->format("l F jS");
I wrote this piece of code in order to display the current date + 2 months :
<?php
$date = date("d/m/Y");
$date = strtotime(date("d/m/Y", strtotime($date)) . "+2 months");
$date = date("d/m/Y",$date);
echo $date;
?>
It does not seem to work as it displays : 01/03/1970.
What am I doing wrong?
Thanks for your help.
EDIT :
After reading comments and answers, I simplified and corrected it.
<?php
$date = date("d/m/Y", strtotime(" +2 months"));
echo $date;
?>
You're missing the second argument for the second strtotime() call:
echo date('d/m/Y', strtotime('+2 months'));
Try using the DateTime object:
$date = new DateTime("+2 months");
echo $date->format("d/m/Y");
If today is "YYYY-mm-31" and next month does not have the 31th day,
it will show the next month of that day, make the system display "+3 months" result instead of "+2 months" result.
So I guess this is the most safety:
$end_date=date("Y-m-d",strtotime("+2 month",strtotime(date("Y-m-01",strtotime("now") ) )));
Change the date to the 1st day first.
Using DateTime->add() or DateTime->modify()
If you are working with an existing DateTime object, you can use one of these:
// Your date
$date = new DateTime(); // empty for now or pass any date string as param
// Adding
$date->add(new DateInterval('P2M')); // where P2M means "plus 2 months"
// or even easier
$date->modify('+2 months');
// Checking
echo $date->format('Y-m-d');
Beware of adding months in PHP, it may overflow to the next month if the day in the original date is higher than the total number of days in the new month. Same overflow happens with leap years when adding years. Somehow this is not considered a bug by PHP developers and is just documented without a solution. More here:
PHP DateTime::modify adding and subtracting months
I found this to be the most to-the-point solution to address overflow problem:
$day = $date->format('j');
$date->modify('first day of +2 months')->modify('+'. (min($day, $date->format('t')) - 1) .' days');