I want to get the timestamp of a day/time for eg
17/12/2014 8pm
Currently I am doing
$curtime = strtotime(date("Y-m-d H:i:s"));
which is giving me the current timestamp but I need to be of 8pm.
Any help is highly appreciated. Thanks in advance.
If you're trying to get a timestamp of today at 8pm, it's actually much more simple than using date since you can use relative times in a strtotime:
$curtime = strtotime('today 8pm');
If you test this with date:
echo date('Y-m-d H:i:s', $curtime); // Returns 2014-12-17 20:00:00
Here's a whole writeup on relative date formats that explains how you can construct proper relative dates.
The best way to do this is using date_create_from_format. Checking out format's parameters, you will end up with something like this:
$date = date_create_from_format('d/m/Y ga', '17/12/2014 8pm');
if (!empty($date)) {//returns false if can't create date
$timestamp = $date->getTimestamp();
//echo date('d/m/Y H:i:s', $timestamp);
}
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).
// to simplify $timestamp in this example is the unix timestamp of 2016-04-20
Consider this example:
strtotime('+1 year', $timestamp); // this returns 2017-04-19
How can I make it return 2017-04-20?
Another example:
strtotime('+1 month', $timestamp); // this returns 2016-05-19
How can I make it return 2016-05-20?
Basically, I want to relatively add time that ends up with the same date.
strtotime('+1 day', strtotime('+1 year', $timestamp));
?
$date = date("Y",$timestamp) + 1 //gives you the next year
$date .= "-" . date("m-d",$timestamp) //concantenates on the current month and day
I may be misunderstanding what you're asking but you're probably better of using the DateTime library built into PHP, it's a lot more flexible than the standard date() function.
So you could do:
$d = new DateTime();
$d->modify('+1 year');
echo $d->format('Y-m-d'); // Outputs: 2017-04-20
If you want to create a DateTime object from a specific date you can do so by:
$d = DateTime::createFromFormat('Y-m-d', '2016-01-01');
echo $d->format('Y-m-d'); // Outputs 2016-01-01
I believe that's what you're after, it's much cleaner than date() and easier to read in my personal opinion.
I cannot use DateTime because of the version of PHP I am running. Can anyone suggest a way to get the equivalent value using strToTime or some other date function. I know this is probably an easy question but I am very rusty on dates in php.
i.e. something like
$date = strToTime('today');
where $date is a date that I can then manipulate by adding hours and so forth along these lines...
$start = $date->format('Y-m-d H:i:s');
$date->modify('+ 60 minutes');
$end = $date->format('Y-m-d H:i:s');
Thanks in advance for any suggestions.
$startTime = strtotime('now');
$endTime = strtotime('+60 minutes', $startTime);
$start = date('Y-m-d H:i:s', $startTime);
$end = date('Y-m-d H:i:s', $endTime);
You can pass a 2nd parameter to strtotime that will be the time that is used when the calculation is made. By default it is time().
strtotime
There are alternatives, that you can get from github, just type php date in search.
For example, you can use moment.php library, which is a clone of moment.js library which is intented to fix problems among different systems, versions etc...
And to perform date calc in moment.php
For example
$m = new \Moment\Moment('2012-05-15T12:30:00', 'CET');
echo $m->addMinutes(60)->format(); // 2012-05-08T13:15:00+0200
Im trying to add a certain amount of days to a timestmp using this in PHP:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo $endDate2;
but its displaying: 1216526400
any ideas?
Try:
echo date("Y-m-d H:i:s",$endDate2);
Or (for just the date):
echo date("Y-m-d",$endDate2);
You can find documentation about how to format your string here: http://php.net/manual/en/function.date.php
You should be using DateTime for working with dates. It's timezone friendly.
$datetime = new DateTime('2008-06-20');
$datetime->modify('+1 day');
echo $datetime->getTimestamp();
strtotime() converts the date into a unix timestamp which is the number of seconds since January 1st 1970. If you want a date output you have to run the finished timestamp through date() first.
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate.' +1 day');
echo date("Y-m-d", $endDate);
strtotime creates a Unix timestamp so if you want to be presented with a formatted date, you need to pass the timestamp as an argument to the date function as follows:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo date('Y-m-d', $endDate2);
Additionally, there are a wide variety of parameters you can use in the date function if you want to display additional information.
e.g.: echo date('Y-m-d H:i:s', $endDate2); or echo date('Y-m-d h:i:s a', $endDate2);, etc.
Sooooo close, just take your timestamp and convert it back into date format using date("desired format",$endDate2);
DateTime is a very nice way to deal with dates. You can try like this:
$capturedDate = '2008-06-20';
$date = DateTime::createFromFormat('Y-m-d', $capturedDate)->modify('+1 day');
echo $date->getTimestamp();
I want to get the date with specific day and time in PHP, like i want the date of next day and time of 9.30 am i.e "2011-06-02 09:30:00".
the code i was using get to do that,
<?php
$next_day_date = date("Y")."-".date("m")."-".(date("d")+1)." 09:30:00";
$new_trig_time_stamp = strtotime($next_day_date);
$trigger_date_time = date("Y-m-d H:i:s",$new_trig_time_stamp);
echo $trigger_date_time;
?>
the code above works fine but fails on 31 day, on 31st it returns "1970-01-01 05:30:00".
Is there any other way to do so.
When shifting dates by a fixed number, it's better to use mktime(), because it handles invalid dates well (e.g. it knows that January 32 is in fact February 1)
$trigger_date_time = date("Y-m-d H:i:s", mktime(9,30,0, date('n'), date('j')+1, date('Y'));
strtotime() is very useful here.
$trigger_date_time = date( "Y-m-d H:i:s", strtotime( "tomorrow 9:30" ) );
Calculate it via unix timestamp - much less annoyance
<?php
$trigger_date_time = date("Y-m-d 09:30:00",time() + 60*60*24);
echo $trigger_date_time;
?>
echo date('Y-m-d', strtotime('+1 day')) . ' 09:30:30';
Have a look at date_add.
In more detail, something like...
$myDate = new DateTime("Some time");
$myDate->add(new DateInterval("P1D"));
You can then use $myDate->format(…) to extract formatted representations.