cant get the number of seconds in PHP - php

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

Related

PHP get current datetime + one year (also asking if secure)

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

Difference between timestamps in minutes in PHP

I want to get difference between 2 timestamps of format Y-m-d H:i:s in minutes in PHP.
The code used is,
$currentDate = date('Y-m-d H:i:s');
$userLastActivity = date($date);
$timeLapse = (($currentDate - $userLastActivity)/60);
Here, $date is obtained from database.
The value of $timeLapse is 0 in output. Please help.
Try using unix timestamp. Practically it measures the time in seconds from 1/1/1970 and it's a lot easier to use and understand than a php object.
$currentTimestamp = new DateTime()->getTimestamp();
$userLastActivity = date($date)->getTimestamp();
$timeLapse = (($currentDate - $userLastActivity)/60);
You should have the time saved as timestamp on the server too, in that case you could use the $date directly as a number, with no need for a conversion. And also, because it's universal, you can pass it around to javascript or any other language without any worries for conversion
Use strtotime to parse textual datetime into a Unix timestamp and substract $userLastActivity from $currentDate and divide by 60.
See if this helps -
<?php
$currentDate = strtotime(date('Y-m-d H:i:s'));
$date = "2016-10-11 02:40:50";
$userLastActivity = strtotime($date);
echo round(abs($currentDate - $userLastActivity) / 60). " minutes";
?>
For more details :strtotime
Change these 2 lines for a start, as I don't think dividing dates by 60 is gonna work to well.
$currentDate = time();
$userLastActivity = strtotime($date);
That way you have time stamps and not dates (string)

get strtotime of specific time in php

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

Setting a date to current using DateTime and modifying it

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

What is the proper way of converting a timestamp to a human readable date?

i've got 2 time stamps: $start_time = 1312346227; and $end_time = 1312346466;
and i am trying to substract them to see the time inbetween $end_time = $end_time - $start_time;
and i get 239.
What is the proper way of converting this to a human readable date?
if i try echo date("h:i:s A",$end_time); i get 04:03:59 and it should be 00:03:59
any ideas?
Thanks
If you have PHP 5.3, use the DateInterval class.
Example stolen from the manual page on DateInterval::format():
<?php
$january = new DateTime('2010-01-01');
$february = new DateTime('2010-02-01');
$interval = $february->diff($january);
// %a will output the total number of days.
echo $interval->format('%a total days')."\n";
// While %d will only output the number of days not already covered by the
// month.
echo $interval->format('%m month, %d days');
?>
You get addiionl four hours, because of your timezone. Remember that unix timestamp is the number of seconds since 1970-01-01 00:00:00 UTC. If you (or your server) are in UTC+4 TZ, then date() will implicitly do a timezone conversion to your local time.
Solution? Use gmdate() instead
You need to set your timezone correctly. http://www.php.net/manual/en/function.date-default-timezone-set.php

Categories