I have a problem with my script and I dont understand where is the problem. So I have this code :
$i_now = strtotime(date('Y-m-d'));
$i_date_last_bonus = strtotime($o_member->date_last_bonus);
$i_datediff = round(abs($i_now - $i_date_last_bonus) / 86400);
print_r("Date Now :".date('Y-m-d'));
print_r("Last Win :".$o_member->date_last_bonus);
I get the $i_datediff = 1 and I dont understand why because in the print_r I have Date Now :2015-12-04 and Last Win:2015-12-03
Can you help me please where I make the error ? Thx in advance and sorry for my english
In one day there are 24 Hrs, in each hour there are 60Min, in each min there are 60sec. Hence, there are 24*60*60 = 86400sec in one day.
Now, The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Means it returns seconds.
so, i_now = 1449187200 andi_date_last_bonus = 1449100800
Difference is 86400sec.
Now $i_datediff = round(abs($i_now - $i_date_last_bonus) / 86400); this is converting seconds in days.
And Difference is 86400 / 86400 = 1
means 1 day.
This result is correct as you get the number of seconds between two dates first and then you divide it by the number of seconds in 24 hours (86400) which gives you 1 (day) as a result.
Related
From a time like:
$time_on_seconds = 18000;
It is equal to 5 hours.
Now for subtract seconds Im using this code:
$new_time_on_seconds = date("H:i", strtotime($time_on_seconds) - 60);
It return me 23:59 when this should return me 4:59
Just remove strtotime.
Strtotime does what the name says it takes a str (string) and makes it time (seconds) so using that on a already number won't work.
$time_on_seconds = 18000;
echo $new_time_on_seconds = date("H:i", $time_on_seconds - 60);
But keep in mind date() is timezone aware so you may end up with a wrong result depending on your timezone.
Set the timezone to UTC and you should get the correct result.
Need some help.
I am using PHP.
So I have coordinates data.
Specifically Longtitude and Latitude.
So let's say I have 15 data of Long and Lat to be inserted on a table.
However, the api gives me only a single datetime because these coordinates are in array.
For example:
[14.4364372;121.0125753, 14.4364375;121.0125755, 14.4364377;121.0125758, 14.436436;121.012574, 14.4364342;121.0125721, 14.4364326;121.0125704, 14.436433;121.0125707, 14.4364334;121.0125711, 14.4364338;121.0125716, 14.4364342;121.012572, 14.4364345;121.0125724, 14.4364348;121.0125728, 14.4364351;121.0125731, 14.4364353;121.0125733, 14.4364356;121.0125735]
So first you will explode it to get rid of the delimeter ','
And loop it to count how many are data,then explode again the delimiter ';' to count the long and lat given.
But it only have a single datetime.
What i want here is to insert these data including datetime but with interval of 30 seconds per insert.
How can i do that?
Expected output would be like this:
INSERT INTO table_gps(ticket,datetime,long,lat) VALUES(0,09/16/2016 03:30:26 pm, 14.4364363,121.0125745)
INSERT INTO table_gps(ticket,datetime,long,lat) VALUES(0,09/16/2016 03:30:56 pm, 14.4364364,121.0125746)
Here is my code:
$msg= 'YC GPS2~09/16/2016 13:29:46~-~[14.4364362;121.0125744, 14.4364363;121.0125745]';
if(strpos($msg,'YC GPS2')!==false){
//explode data
$explode=explode('~',$msg);
$ky=$explode[0];
$datetime=$explode[1];
$ticket=$explode[2];
$coords=$explode[3];
$coords=explode(',',$coords);
for($i=0;$i<count($coords);$i++){
$xpVal=$coords[$i];
if($xpVal){
$xp3=explode(';',$xpVal);
$lng=$xp3[0];
$lat=$xp3[1];
$lng=str_replace('[','',$lng);
$lat=str_replace(']','',$lat);
$time = date("m/d/Y h:i:s a", time() + 30);// where 30 is the seconds
echo "INSERT INTO GPS2(ticket,datetime,long,lat) VALUES(".$ticket.",".$time.",".$lng.",".$lat.") ";
}
}
}else{
echo '0';
}
Thanks.
In order to add how many seconds you want to particular date in PHP you can use the following.
$time = date("m/d/Y h:i:s a", time() + 30);// where 30 is the seconds
The following is really easy way to add days, minutes, hours and seconds to a time using PHP. Using the date function to set the format of the date to be returned then using strtotime to add the increase or decrease of time then after a comma use another strtotime passing in the start date and time.
//set timezone
date_default_timezone_set('GMT');
//set an date and time to work with
$start = '2014-06-01 14:00:00';
//display the converted time
echo date('Y-m-d H:i',strtotime('+1 hour +20 minutes',strtotime($start)));
Times can be entered in a readable way:
+1 day = adds 1 day
+1 hour = adds 1 hour
+10 minutes = adds 10 minutes
+10 seconds = adds 10 seconds
I am trying to set publish date based on user choice and give it interval in a loop. But after it is substituted with the intervals, the year changed to the current year.
Here is the sample of my code:
$datestart = "2012-03-06";
$datenow = date("$datestart H:i:s", current_time( 'timestamp' ));
$newpostdate1 = $datenow + strtotime("0 years 0 months 1 days 0 hours 0 minutes 0 seconds");
$newpostdate = date("Y-m-d H:i:s", $newpostdate1);
echo $datenow . " " . $newpostdate;
$datenow Will return 2012-03-06 16:19:33 while $newpostdate return the current date plus 1 day i.e: 2014-03-15 17:02:23.
Why $newpostdate returning the current date plus next 1 day instead of 2012-04-06 16:19:33 ?
..because what you're doing doesn't do what you think it does.
First, you set $datenow to a string (not a date object), with value "2012-03-06 " + the current time (assuming that's what current_time returns).
Then you call strtotime with the value "1 days" (well, your string has a bunch of other zero-valued fields, but they don't change the result), which returns the current time + 24 hours as a number (the number of seconds since 1970).
Then you take that value and add it with + to the above string. This causes the string to be interpreted as a number, so it turns into 2012 (and the rest of the string is ignored). So the result is a timestamp representing the current time + one day + 2,012 seconds - or one day, 33 minutes and 32 seconds from the time the code is run. Which you then format as a string.
You could use John Conde's solution to get a more meaningful result. (I assume your real problem is different, else why not just start out by setting the string to '2012-03-07' in the first place?)
The first parameter of date() is the format you want the timestamp passed as the second parameter to be displayed as. So basically you are using date() incorrectly.
I think this is what you are looking for:
$date = new DateTime($datestart);
$date->modify('+1 day');
echo $date->format(Y-m-d H:i:s);
I've two timestamps with a gap between a few hours and a few days.
Is there a way in PHP and / or MySQL to get the time slice for every day?
INPUT
Timestamp 1: 2013-10-30 10:00:00
Timestamp 2: 2013-10-31 11:00:00
OUTPUT
2013-10-30: 14 hours
2013-10-31: 11 hours
Easiest way to perform operations on dates is to convert them to timestamps.
Here, what you can do is to get three timestamps :
$ts1 = strtotime("2013-10-30 10:00:00");
$ts2 = strtotime("2013-10-31 00:00:00");
$ts3 = strtotime("2013-10-31 11:00:00");
$first_time_slice = ts2 - ts1; //here you got your first time slice in seconds, convert it to hours
$second_time_slice = ts3 - ts2; //idem for the second time slice
It can be done in MySql like this:
SELECT TIMEDIFF(TIMESTAMP(DATE('2013-10-30 10:00:00') + INTERVAL 1 DAY), '2013-10-30 10:00:00'),
TIMEDIFF('2013-10-31 11:00:00', TIMESTAMP(DATE('2013-10-31 11:00:00')))
i my php codes i do time()-86400 to fetch everything from the last 24 hours, but how i can get everything today or everything from yesterday. thus it is no longer 86400 seconds, it should be after 12 midnight till current time.
hope this makes sense.. but how i can do this?
If you are "fetching" from a database, why not do it in the query?
SELECT * FROM `table` WHERE DATE(`created_at`) = '2011-03-28';
If you are storing the date as a unix timestamp:
SELECT * FROM `table` WHERE DATE(FROM_UNIXTIME(`created_at`)) = '2011-03-28';
time()-strtotime('today') - difference between now and midnight; time()-strtotime('yesterday') - difference between now and yesterday midnight; time()-strtotime('-2 days')...
for yesterday only (range $min to $max)
$start = strtotime('yesterday')
$end = strtotime('today') - 1;
etc.
Following will give you the seconds passed since January 1, 1970. Every object with a timestamp higher than this value is from the current day (given that you have set your timezones and local time correctly).
$time = strtotime(date('Y-m-d 00:00:00'));
You can use the PHP date and strtotime function in order to pick a day from now and retrieve the seconds that specific date. For more info, see: http://php.net/manual/en/function.date.php and http://php.net/manual/en/function.strtotime.php
I agree with Gordon here - there are so many date/time examples. But hey, let's go over it again - assuming today begins at midnight, you use:
$start = strtotime('today');
Assuming "today" ends at 23:59, simple arithmetics imply that if you increment the $start by 24 hours and take away 1 second - you'll reach the end of today.
So:
$start = strtotime('today');
$end = $start + (3600 * 24) - 1;