Add 1 Week in DateTime to Create Expiry DateTime - php

I'm Getting Saved Date/Time from Database in Following Format,
$date_time = date("j M, Y, g:i a"); //1 Feb, 2015, 12:00 am
Looking for a solution to add 7 days in $date_time to create expiry date, Tried several solutions discused on stackoverflow but none of them is working for me,
Any help will be appriciated. Thanks
Edited
#panther answered partially worked with only Date
$date_time = date("j M, Y");
but with Date/Time
$date_time = date("j M, Y, g:i a");
adding 7 days
$end_date = date("j M, Y, g:i a", strtotime($date_time . ' + 7 days'));
will return the result "31 Dec, 1969, 12:00 am"
So I tried with only Date to calcualte the difference (Inside PHP While Loop)
<?php if ($end_date>$date_time) { ?>
Expired
<?php } else { ?>
Active
<?php } ?>
And it didn't work, either it sets all records to Active or Expired,
So I'm Back to Sqaure 1.
Note: I tried with UNIX_TIMESTAMP but the end result same, either sets all records to Active or Expired.

Try this:
$date_time = date("j M, Y, g:i a");
$end_date = date("j M, Y, g:i a", strtotime($date_time . ' + 7 days'));

Using DateTime is also a good option:
$date_str = "..."; // from database
$date_format = "j M, Y, g:i a";
$expire_date = DateTime::createFromFormat($date_format, $date_str);
$expire_date = $expire_date->add(DateInterval::createFromDateString('+7 days'));
and if you need it formated back to string:
$expire_date->format($date_format);

You could try using PHPs DateTime Object to get the current Time
$date_time_obj = new \DateTime();
$date_time = $date_time->format('j M, Y, g:i a');
And then use the add() method to get your expiry time
$end_date_obj = clone $date_time_obj;
$end_date_obj = $end_date_obj->add(new \DateTimeInterval('P7D'));
$end_date = $end_date_obj->format('j M, Y, g:i a');
A little more complicated than the other answers, i know :) But maybe also a little more helpfull in the future!
Cheers

Related

How to correctly compare dates in php with the same format

Trying to do a simple date comparison. Both dates are in the format of "Y F jS, g:i a". e.g. 2018 September 3rd, 9:30 am.
$today = date('Y F jS, g:i a');
$expire = file_get_contents(dirname(__FILE__).'/maintenance/end_time.php');
if($today>$expire){
unlink(dirname(__FILE__).'/maintenance/end_time.php');
}
The code I have doesn't work correctly. I also tried strototime() but that was also unsuccessful.
Try this.
$today_dt = new DateTime($today);
$expire_dt = new DateTime($expire);
if ($today_dt > $expire_dt)
{ /* Do something */ }
Update: It might possible that the date string is not supported by DateTime parser so you can initialize your dates like this.
$today_dt = DateTime::createFromFormat('Y F jS, g:i a', $today);
$expire_dt = DateTime::createFromFormat('Y F jS, g:i a', $expire);
Use strtotime
$today = time();
$expire = strtotime(file_get_contents(dirname(__FILE__).'/maintenance/end_time.php'));
if($today>$expire){
//code
}
OR
$today = date('Y F jS, g:i a');
$expire = file_get_contents(dirname(__FILE__).'/maintenance/end_time.php');
if(strtotime($today) > strtotime($expire)){
unlink(dirname(__FILE__).'/maintenance/end_time.php');
}

php change the string into time format

In php I have time like this
$time = '2015-06-29T16:00:00Z';
I want to convert that time like this format Tuesday, December 16, 2015 3:00 PM
For that I tried
echo date( 'jS F Y', strtotime( $time) );
but it is showing time like 1st January 1970
So can someone help me to get the actual time format as I want.
A simple DateTime class usage should suffice, just feed it into the constructor, the just use ->format and provide the desired output format:
$time = '2015-06-29T16:00:00Z';
$date = new DateTime($time);
echo $date->format('jS F Y');
Sample Output
You can use the DateTime class for better handling of dates
$time = '2015-06-29T16:00:00Z';
$dateTime = new DateTime($time);
echo $dateTime->format('l, F d, Y g:i A');
$time = '2015-06-29T16:00:00Z';
echo date( 'l, F j, Y H:i A',strtotime($time));
l, F j, Y H:i A can be re-ordered to change the output.
About date function, http://php.net/manual/en/function.date.php
Just pass proper format parameters to it.
$time = '2015-06-29T16:00:00Z';
echo date( 'l, F j, Y g:i A', strtotime( $time) );
Use preg_split:
$parts = preg_plit("/Z/",$time);
$parts = preg_split("/T/",$parts[0]);
$theDate=$parts[0];
$theTime=$parts[1];
$what_you_want=date(strtotime($theDate." ".$theTime);
Note that you can still change the format of the output.

Date Format is not working

i have a string that is like this 2012/10/12 10:03:46 (Year/Month/Day) format now i want to change its format to something like this October 12, 2012 10:03 p.m i have tried php's DateTime class but its not working:
<?php
$date = new DateTime();
$date->createFromFormat('Y/m/dd H:i:s', substr($suggestion->suggestion->created_at, 0,19));
echo $date->format('d-m, y h:i A');
?>
can anyone tell me whats wrong and how can i correct it??
You should have seen at least one notice something like "don't call static function in non-static context".
DateTime::createFomFormat()
$date = DateTime::createFromFormat('Y/m/d H:i:s', substr($suggestion->suggestion->created_at, 0,19));
echo $date->format('F j, Y, g:i a');
It's a static method and (as the name suggests) it creates a new instance of DateTime.
$today = date("F j, Y, g:i a");
<?php
$date = new DateTime('2012/10/12 10:03:46');
echo $date->format('d-M, y h:i:s A');
//output - 12-Oct, 12 10:03:46 AM
?>
Try this...
$today = date("F j, Y h:i a"); // gives October 15, 2012 5:20 PM
Try this...
$today = date("F j, Y g:i A"); // gives October 15, 2012 5:20 PM (g removes leading zeroes)
Your case
$date = new DateTime('2012/10/12 10:03:46');
echo $date->format('F j, Y h:i A'); // gives October 15, 2012 10:03 AM
Reference : click

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 Convert a Date into this format [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP convert one date into another date format
This is PHP
I have this result:
2011-09-20 13:00:00
I want to convert it into this format:
September 20 2011 1:00 pm
Do you know how to do that?
Anyhelp would be greatly appreciated.
Thanks!
try this:
$old_date = '2011-09-20 13:00:00';
$old_date_timestamp = strtotime($old_date);
$new_date = date('F j Y g:i a', $old_date_timestamp);
If that result comes from an object of type DateTime you can use the format function:
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s');
and here all the formats you can have.. change it according to your needs.
You can get the UNIX-timestamp of a time string with strtotime() and you can get a differently designed time string with strftime()
For more information you can read the documentation here : http://php.net/manual/en/function.date.php
But also is simple. Lets see how
$d = "2011-09-20 13:00:00";
$d = strtotime($d);
$d = date("F m Y g:i a", $d);
echo $d;
Take a look at these PHP functions:
http://nl.php.net/manual/en/function.date.php
and
http://nl.php.net/strtotime
To do something like this:
date('F d Y G:i',strtotime("2011-09-20 13:00:00")); // your required format
You can use this format
$date= date("F j Y g:i a");
if you have date in any variable then
$date= date("F j Y g:i a",strtotime($your_variable));
echo $date
echo date("F d Y g:i a",strtotime("2011-09-20 13:00:00 "));
echo date('F d Y g:i a', strtotime('2011-09-20 13:00:00'));
check the date format

Categories