Removing hh:mm:ss from PHP's strtotime function - php

I'm setting up a HighCharts line graph and the x axis labels won't match up with the data points. The data points roll up to the whole day (no hours). When I add the pointStart, though, I'm getting the unix timestamp for when I run it:
pointStart: <?php echo strtotime("-1 month -1 day") * 1000; ?>
Can I say "give me the unix timestamp for a month and a day ago at midnight" without complicating things?

Of course you can :
<?php echo strtotime(date('Y-m-d', strtotime("-1 month -1 day"))); ?>
The date() format will remove hours minutes and seconds, then you can re-get your timestamp

Related

how to get future today to future calculate with days date in php

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

PHP date() is wrong when using a UTC timestamp

$dayBasedOnUTC = date('l', $_GET['day']);
Why is it that when I echo the value of $dayBasedOnUTC the day returned is a Tuesday?
The UTC value of $_GET['day'] is: 1409393126144
If you put that number into any Unix Timestamp Converter you will see the date is Saturday.
It appears that 1409393126144 is a Javascript timestamp, which is counted in milliseconds. PHP expects its UNIX timestamps in seconds though. So 1409393126144 to PHP is a timestamp in the far future.
Divide by 1000 to get the correct value:
echo date('l', 1409393126144 / 1000);

subtract a few hours from a date in php

I am trying to echo the timestamp for a file using this:
Last updated: <?= date("m/d/Y H:i:s",filemtime("file1.html")) ?>
but this is on a server that is 6 hours ahead.
I've tried using DateTime::sub or date_sub or sub but none of these are being recognized. Do I need to call the date class or something?
filemtime returns a timestamp, just subtract the number of seconds you need.
date("m/d/Y H:i:s",filemtime("file1.html")-6*3600)
or specify the timezone
$time = new Datetime(new Datetimezone('America/Chicago'));
$time->setTimestamp(filetime("file1.html"));
echo $time->format('m/d/Y H:i:s');

Compare timestamp to date

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);

how to subtract two dates and times to get difference

i have to sent an email when a user register email contain a link that is become invalid after six hours
what i m doing when email is sent i update the db with field emailSentDate of type "datetime"
now i got the curent date and time and has made to the same formate as it is in db now i want to find that both these dates and time have differenc of 6 hours or not so that i can make link invalid but i donot know how to do this
my code is look like this i m using hardcoded value for db just for example
$current_date_time=date("Y-m-d h:i:s");
$current=explode(" ",$current_date_time);
$current_date=$current[0];
$current_time=$current[1];
$db_date_time="2010-07-30 13:11:50";
$db=explode(" ",$db_date_time);
$db_date=$db[0];
$db_time=$db[1];
i do not know how to proceed plz help
<?php
//$now = new DateTime(); // current date/time
$now = new DateTime("2010-07-28 01:11:50");
$ref = new DateTime("2010-07-30 05:56:40");
$diff = $now->diff($ref);
printf('%d days, %d hours, %d minutes', $diff->d, $diff->h, $diff->i);
prints 2 days, 4 hours, 44 minutes
see http://docs.php.net/datetime.diff
edit: But you could also shift the problem more to the database side, e.g. by storing the expiration date/time in the table and then do a query like
... WHERE key='7gedufgweufg' AND expires<Now()
Many rdbms have reasonable/good support for date/time arithmetic.
What you can do is convert both of your dates to Unix epoch times, that is, the equivalent number of seconds since midnight on the 31st of December 1969. From that you can easily deduce the amount of time elapsed between the two dates. To do this you can either use mktime() or strtotime()
All the best.
$hoursDiff = ( time() - strtotime("2010-07-30 13:11:50") )/(60 * 60);
I'd rather work with a timestamp: Save the value which is returned by "time()" as "savedTime" to your database (that's a timestamp in seconds). Subtract that number from "time()" when you check for your six hours.
if ((time() - savedTime) > 6 * 3600)
// more than 6h ago
or
"SELECT FROM table WHERE savedTime < " . (time() - 6 * 3600)
This might be the solution to your problem -> How to calculate the difference between two dates using PHP?

Categories