I'm trying to 1) set a variable to the current date 2) format it as Y-m-d and 3) modify it to find a date 7 days in the past.
Here is the code I'm using to do this:
$date = new DateTime(); // get current date
$date->format('Y-m-d'); // format it
$wow_date = $date->modify('-7 days'); // find 7 days before current date
When I run this I get a 500 error code and haven't been able to troubleshoot why this is happening. Would greatly appreciate if someone could point me in the right direction.
UPDATE
Thanks for the help / comments. Here is what I've ended up going with:
$date = new DateTime(null, new DateTimeZone('America/Los_Angeles')); // get current date
$m_date = $date->format('Y-m-d'); // set it in format I need for queries
$wow_date = $date->modify('-7 days'); // get 7 days before
$m_wow_date = $wow_date->format('Y-m-d'); // format earlier date
If you want to use object oriented style, try this
$date->sub(new DateInterval('P7D'));
This is from php.net
Answered in this SO thread:
$date = date('Y-m-d', strtotime('-7 days'));
Or with DateTime class:
$date = new DateTime('7 days ago');
echo $date->format('Y-m-d');
If your final goal is a variable of type String containing the formatted date a week ago, then you can do it all in one line:-
$formattedDate = (new \DateTime())->modify('-7 days')->format('Y-m-d');
echo $formattedDate;
See it working
You need to configure default timezone for your application.
Try to find it on your php.ini uncomment or add this lines:
[Date]
; Defines the default timezone used by the date functions
date.timezone = "America/Los_Angeles"
Or via php script before use DateTime Class:
date_default_timezone_set('America/Los_Angeles');
Find your desired timezone at PHP MANUAL
Related
just trying to create a variable where it outputs the exact current datetime and adding exactly one additional year. how do i add the days?
$expirationdate = date('Y-m-d H:i:s', .' + 1 year'));
Lets say the exact current date is 2019-01-23 17:11:25
the variable will be: 2020-01-23 17-11-25 (+365)
Also, if a person manually modifies the date on their PC/Phone, will that time be the start of the current date on the variable?
You can achieve your result by using strtotime() and date() function of php
$expirationdate = date('Y-m-d H:i:s', strtotime('+ 1 year'));
print_r($expirationdate);
You can read more about the strtotime() and date()
Try with below code:
$expirationdate = date('Y-m-d H:i:s', strtotime('+1 years'));
I hope it will help you.
Try This :
$date= date('Y-m-d H:i:s',strtotime('+1 years'));
I think the best way to work with dates and time in PHP is through the DateTime object. You can use modify method to add or subtract days, years or whatever you want, like this:
$d = new DateTime(); //current date
$d->modify('+10 days');
$d->modify('+1 year');
echo $d->format('Y-m-d H:i:s');
Regarding your second question if a person modifies date on his computer it won't change anything because PHP runs on the server and takes date from it (not from the user machine).
What I want to do:
Getting a certain day from a certain month directly via the DateTime methods (no mktime stunts :)), like
$day = new DateTime('15th of next month');
but it's not possible to set a fixed day by it's number.
Can anybody help ?
EDIT: I've changed the DateTime from this month to next month to make the problem more clear.
You can use DateTime::createFromFormat(). If you only pass the day value, it'll default to the current month and year.
$date = DateTime::createFromFormat('d', '15');
echo $date->format('Y-m-d'); // 2018-10-15
Edit for the new question requirements:
$date = DateTime::createFromFormat('d', 15)->add(new DateInterval('P1M'));
echo $date->format('Y-m-d'); // 2018-11-15
I have the following code to get the current date and hour:
date_default_timezone_set('Asia/Kabul');
$today = date('Ymdh');
echo $today; // prints 2015020212 Fine
I was wondering if I could get it's number of seconds! But the following does not give me that.
echo strtotime($today);
I really need to get date 2015020212 in seconds! please help me!
The reason why strtotime is not returning what you expect is because you need to give it the date in the correct format:
$today = date('Y-m-d');
strtotime($today);
and
$today = date('Y-m-d H:i:s');
strtotime($today);
will work, but you have given it Ymdh, which PHP has no idea how to interpret.
For more info on valid formats check out the page in the PHP manual.
You can use date('Ymdh h:i:s') if you're looking for the local time as well.
h would give the 12 hour format of the hour with leading zeroes;
i for minutes; and
s for seconds.
Source : PHP Date Manual
You will probably need something like this:
$date = new DateTime();
$time_in_seconds = $date->getTimestamp();
I'm being passed a date string (most likely in ISO8601 format) and need to convert it to the date of the ISO week to store as a DATETIME column in MySQL. To initialize the DateTime object the I want to save, I'm doing the following:
$date = new DateTime("now");
$date = new DateTime( $date->format("o-\WW") );
echo $date->format(DateTime::ISO8601) . "\n";
Since I'm using Doctrine2, I need to pass my entity a DateTime object. Is there a way to avoid making 2 DateTime objects to get the same result? Should I drop back to the date function and use that as the argument to the DateTime constructor?
$date = new DateTime( date("o-\WW", strtotime("now") );
You could use setISODate to update the first DateTime object using the week and year of the object via format():
$date = new DateTime();
$date->setISODate($date->format('o') , $date->format('W'));
echo $date->format(DateTime::ISO8601);
You could use modify() method of DateTime object.
$date = new DateTime();
$date->modify('sunday this week');
echo $date->format(DateTime::ISO8601) . "\n";
Note that if you want the first day of the week to be something other than Sunday, you will likely need to do something like the following. This example considers Monday as the first day of the week, thus for dates on a Sunday, you would need to get the date of the Monday from the previous week.
$date = new DateTime();
if ($date->format('D') === 'Sun') {
$date->modify('monday last week');
} else {
$date->modify('monday this week');
}
echo $date->format(DateTime::ISO8601) . "\n";
You can probably use date like this:
$date = new DateTime( date('o-\WW') );
Though that formatting looks a bit strange. :p You can of course also use some other method/function the class has to offer to change/modify the date.
I don't know how to explain this correctly but just some sample for you guys so that you can really get what Im trying to say.
Today is April 09, 2010
7 days from now is April 16,2010
Im looking for a php code, which can give me the exact date giving the number of days interval prior to the current date.
I've been looking for a thread which can solve or even give a hint on how to solve this one but I found none.
If you are using PHP >= 5.2, I strongly suggest you use the new DateTime object, which makes working with dates a lot easier:
<?php
$date = new DateTime("2006-12-12");
$date->modify("+7 day");
echo $date->format("Y-m-d");
?>
Take a look here - http://php.net/manual/en/function.strtotime.php
<?php
// This is what you need for future date from now.
echo date('Y-m-d H:i:s', strtotime("+7 day"));
// This is what you need for future date from specific date.
echo date('Y-m-d H:i:s', strtotime('01/01/2010 +7 day'));
?>
The accepted answer is not wrong but not the best solution:
The DateTime class takes an optional string in the constructor, which can define the same logic as the modify method.
<?php
$date = new DateTime("+7 day");
For example:
<?php
namespace DateTimeExample;
$now = new \DateTime("now");
$inOneWeek = new \DateTime("+7 day");
printf("Now it's the %s", $now->format('Y-m-d'));
printf("In one week it's the %s", $inOneWeek->format('Y-m-d'));
For a list of available relative formats (for the DateTime constructor) take a look at http://php.net/manual/de/datetime.formats.relative.php
If you are using PHP >= 5.3, this could be an option.
<?php
$date = new DateTime( "2006-12-12" );
$date->add( new DateInterval( "P7D" ) );
?>
You will have to look into strtotime(). I'd imagine your final code would look something like this:
$future_date = "April 16,2010";
$seconds = strtotime($future_date) - time();
$days = $seconds /(60 * 60* 24);
echo $days; //Returns "6.0212962962963"
You can use mktime with date. (http://php.net/manual/en/function.date.php)
Date gives you the current date. This is better than simply adding/subtracting to a timestamp since it can take into account daylight savings time.
<?php
# this gets you 7 days earlier than the current date
$lastWeek = mktime(0, 0, 0, date("m") , date("d")-7, date("Y"));
# now pretty-print it out (eg, prints April 2, 2010.)
echo date("F j, Y.", $lastWeek), "\n";
?>