How to find 1 day ago time in PHP microtime() format.
I am trying like this. But still showing the current time.
$date = date("Y-m-d H:i:s", strtotime('-1 day'));
$val = microtime($date);
http://us2.php.net/manual/en/function.microtime.php
microtime() returns the current Unix timestamp with microseconds
You can't use microtime() to get a time from the past, since the funtion only works with current date.
Wouldn't this work for you?
$date = strtotime('-1 day');
Edit: Code corrected according to u_mulder's comment.
Related
I have 2 variables in PHP. One is getting today's date and adding 1 month to it. That works fine. The other is supposed to take that date and add 6 days to it. For some reason that part refuses to work. Am I simply formatting it wrong? I always get 01-06-1970 in my database.
Here is the variable that gets today's date and adds 1 month (works fine)
$renewdate = date('Y-m-d', strtotime('+1 month'));
Here is the variable that adds 6 days to $renewdate (does not work)
$latedate = date('Y-m-d', strtotime('+6 days',$renewdate));
Second argument of strtotime is Unix timestamp. Currently $renewdate is a string. So:
$latedate = date('Y-m-d', strtotime('+6 days', strtotime($renewdate)));
PHP 5.2.0 brought DateTime, why are you still sticking to old functions? OOP approach is better!
$dtCreate = DateTime::createFromFormat('Y-m-d H:i:s', '2016-08-02 16:16:02');
$dtCreate->add(new DateInterval('P6D'));
This will add 6 days to your DateTime object, see DateInterval for details.
After you added an interval, you may format your object however you wish:
$dtCreate->format('Y-m-d H:i:s');
This will return 2016-08-08 16:16:02, as you can see, it's 6 days later.
How to calulate Future date.
Today date is : 2015-09-05
if day is 120, then what will be the, future date.
I was creating Program in php, which user pay Online,
We will give validity date by 120days from paid amount date.
Then what will be the validity date.
I will accept that person answer me correct answer 1st
The following will get you the date 120 days into the future:
strtotime("+120 days");
The strtotime function also takes a second parameter, which can be used if you want to add 120 days to a specific date instead of to the current date.
See the official documentation for more information.
$expirydate = date('Y-m-d G:i:s', strtotime('+120 days'));
'time()' will give you unix timestamp (miliseconds elapsed since 1.1.1970).
120 days in miliseconds can be calculated like this '120*24*60*60'.
You can get current date by 'date($format)' and then add offset to it 'date($format, $offset)'.
Here is an example:
<?php
$future = time() + (120*24*60*60);
$format = "Y-m-d";
$now = date($format);
$future_date = date($format, $future);
?>
You can also check similar example here
I need to convert a date and time (GMT) into a timestamp with php. The following code shows what I'm currently using:
<?php
$date="2012-06-29 10:50";
$timestamp = strtotime($date);
echo $timestamp;
?>
However, when I test the timestamp in an online convertor (http://www.epochconverter.com), the resulting date is 29th June 2012, 8:50 GMT, or 2 hours previous. Is it possible that the strtotime() function isn't completely accurate and is just an estimate of the time? If so, are there better methods I could use for getting the exact time?
Thanks.
strtotime assumes that you are converting a string in your server's local time, so if the servers time zone is two hours out the result will be as wll.
The comments in the manual suggest a couple of solutions, you can append UTC to your date:
$timestamp = strtotime($date.' UTC');
Or you can change the default timezone for the script (this will apply to all other time functions!):
date_default_timezone_set('UTC');
$timestamp = strtotime($date);
As a final alternative, you could try date_create_from_format which allows you to specify what exactly the format your string is:
$datetime = date_create_from_format('Y-m-d H:i', $date, new DateTimeZone('UTC'));
$timestamp = date_format($datetime, 'U');
// Alternatively (thanks Herbert) - 5.3+ only
$timestamp = date_timestamp_get($datetime);
I want to get the unix time stamp for that day exactly 30days back from current day.
What is the best method?
Can i use this to get the date 30 days back, is this the best method?
$day = date('Y-m-d', strtotime('-30 days'));
a google search brings me to mktime() function in php. But how do i combine both and get the unix time stamp for the day? What is the easiest and fastest method?
You just need to use the strtotime("-1 month"); function. That will return a UNIX timestamp.
$date = date_create();
date_sub($date, date_interval_create_from_date_string('1 m'));
echo date_format($date, 'U');
How to get previous month and year relative to today, using strtotime and date? watch this question. there is the discussion about the funny strtotime("-1 month"); bug.
I need to compare a timestamp to a date. I would just like to compare the date portion without the time bit. I need to check whether a timestamp occurs on the day before yesterday i.e. today - 2.
Could you show me a snippet please? Thank you.
I've been reading through the PHP docs but couldn't find a very clean way of doing this. What I found was converting the timestamp to a date with a particular format and comparing it to a date which I get by doing a time delta to get the date before yesterday and converting it to a particular format. Messy.
You can arcieve this by using the function strtotime.
To round to a day I personaly like to edit the timestamp. This is a notations of seconds since epoch. One day is 86400 seconds, so if you do the following caculation:
$time = $time - ( $time % 86400 );
You can convert it back to a date again with the date function of PHP, for example:
$readableFormat = date( 'd-m-Y', $time );
There is also much on the internet about this topic.
you can use the strtotime function
<?php
$time = strtotime("5 june 2010");
$before = strtotime("-1 day",$time);
$after = strtotime("+1 day",$time);