PHP Odd time when adding - php

To get time I do this
$day_timmer = time()
echo $day_timmer;
This is what printsout 1332533147
But when I add to it I get this
$day_timmer = time() * 1000;
echo $day_timmer;
This is what printsout 1.332533387E+12
No clue what I'm doing wrong.
So I get voted down for wondering why letters are in my numbers lol

That's called scientific notation, and it's all right. E+12 stands for 1012 and in your case, this is exactly the given number:
1.332533387E+12 = 1.332533387 * 10^12 = 1,332,533,387,000

To fix your problem use the following statement.
<?php
echo date("h:i a");
?>

Related

setting accountexpires date with PHP and LDAP

I'm having trouble setting the 'accountexpires' and wondered if anyone knows a way around it, I believe it's because I have a 18 digit number. I have a form where someone sets a date, I need to set the account to expire then, if I provide the number as a hardcoded string it works, but I cannot convert the finished calculation to a string. Anyone know any way around this? thanks.
<?php
$currentTimeUnix = 1442383989; //this will vary depending on user's input
$secondsBetween1601and1970 = 11644473600;
$timesAdded = $currentTimeUnix + $secondsBetween1601and1970;// comes to 13086857589
$nanoseconds = $timesAdded * 10000000; //comes to 130868575890000000
echo $nanoseconds; //displays 1.3086857589E+017
echo (string)$nanoseconds; //displays 1.3086857589E+017
echo strval($nanoseconds); //displays 1.3086857589E+017
?>
PHP is kind of bad with large numbers but sprintf can help. try:
echo sprintf( '%018.0f', $nanoseconds );
-> 130868575890000000
Alternatively you change the "precision" setting where precision is described:
precision integer
The number of significant digits displayed in floating point numbers.
ini_set("precision",20);
echo $nanoseconds;
-> 130868575890000000
Thanks for this. Been testing true out the net but nothing worked until i stumbled on this solution:
$expiredate = strtotime($values["expires"]);
$currentTimeUnix = $expiredate;
$secondsBetween1601and1970 = 11644473600;
$timesAdded = $currentTimeUnix + $secondsBetween1601and1970;
$nanoseconds = $timesAdded * 10000000;
$expires = sprintf('%018.0f',$nanoseconds);

compare times with hundredths in php

I try to compare two swim times in php. They are like HH:MM:SS.XX (XX are hundreths). I get them as string and i want to find out which swimmer is faster. I tryed to convert them using strtotime(). It works with hours, minutes and seconds but it ignores hundreths. Here is my code for better explanation:
$novy = strtotime($input1);
$stary = strtotime($input2);
if($novy < $stary){
//change old(stary) to new(novy)
}
If $input1 is 00:02:14.31 and $input2 is 00:02:14.32 both $novy and $stary are 1392850934.
I read some solution to similar problem in javascript but I can`t use it, this must be server-side.
Thank you for help.
If you use date_create_from_format you can specify the exact date format for php to convert the string representations to:
<?php
$input1 = '00:02:14.31';
$input2 = '00:02:14.32';
$novy = date_create_from_format('H:i:s.u', $input1);
$stary = date_create_from_format('H:i:s.u',$input2);
if ($novy < $stary) {
echo "1 shorter\n";
} else {
echo "2 longer\n";
}
Recommended reading: http://ie2.php.net/datetime.createfromformat
If the format is really HH:MM:SS.XX (ie: with leading 0's), you can just sort them alphabetically:
<?php
$input1 = '00:02:14.31';
$input2 = '00:02:14.32';
if ($input1 < $input2) {
echo "1 faster\n";
} else {
echo "2 faster\n";
}
It prints 1 faster
You could write some conditional logic to test if HH::MM::SS are identical, then simply compare XX, else use the strtotime() function that you are already using
You are working with durations, not dates. PHP's date and time functions aren't really of any help here. You should parse the string yourself to get a fully numeric duration:
$time = '00:02:14.31';
sscanf($time, '%d:%d:%d.%d', $hours, $minutes, $seconds, $centiseconds);
$total = $centiseconds
+ $seconds * 100
+ $minutes * 60 * 100
+ $hours * 60 * 60 * 100;
var_dump($total);
The total is in centiseconds (100th of a second, the scale of your original input). Multiply/divide by other factors to get in others scales, as needed.

Echo text directly after variable

I have 2 variables named $min and $sec.
If $min = 5 and $sec = 15 I want to print this "5m 15s", echo "$min m $sec s";
But that gives "5 m 15 s". How do I get rid of the blank space?
You can use braces to do this, same as many shells:
$min = 7;
$sec = 2;
echo "${min}m ${sec}s";
The output of that is:
7m 2s
The braces serve to delineate the variable name from the following text in situations where the "greedy" nature of variables would cause problems.
So, while $minm tries to give you the contents of the minm variable, ${min}m will give you the contents of the min variable followed by the literal m.
echo $min."m ".$sec."s"; is one way of doing it
u can try this
echo $min."m ".$sec."s ";
edit>
the output is
5m 15s

Divide money value for payment (PHP)

First I thought this was a stupid question, and i should do some search and it would be easy to solve. But I am afraid I just ain't getting anywhere!
The thing i need to do is simple. I have a U$ value and i want to divide it by 12. Thats it.
Well, the thing is that this value is outputed by a function, and echoes ok, look:
<?php
$preconormal = wpsc_the_product_price(); // it echoes like 99.90
$precoja = str_replace (".", "", $preconormal);
echo $precoja; //echo ok -> 9990
$quantas = '12';
$parcela = $precoja/$quantas; // ok, so divide 9990 by 12, right?
echo $parcela; //no!!!!! it echoes 0 :(
?>
I really hope you can help me!
You are trying to divide strings, if you used numbers say
$quantas = 12;
$precoja = 9990;
What happens?
It should fix the division, in which case, prior to the mathmematics, convert your vars to integs by
$quantas = intval($quantas);
$precoja = intval($precoja);
//your manipulation here..l
Remove the quotes...
$quantas = '12';
to
$quantas = 12;
$precoja = floatval($preconormal)*100;
$preconormal = $precoja / 12;
I'd change your 5th line by removing the single quotes and/or 6th line with $parcela = (int)$precoja / (int)$quantas; because as soon as you use the function str_replace then $precoja becomes a string. Also having the single quotes earlier on = '12' it is also a string and that division returns 0.

Subtraction with php and mysql

I am developing a joomla extension and in default.php file i have this code:
foreach($this->subv as $subv) {
$giorni = ((int)$subv->data_fine - (int)$subv->data_inizio);
$ore = ($giorni * 24) % 24;
echo $giorni.' : '.$ore;
}
$this->subv is an object that contains the result of a mysql query. My problem is that echo prints $subv->data_fine value, and not the result of the substraction. $subv->data_fine and $subv->data_inizio contain the result of time() function.
How can i resolve it this problem?
Thanks!
If I understand you problem correctly, $giorni is equal to $subv->data_fine, which would simply mean that (int)$subv->data_inizio evaluates to zero. Have you checked that?
You can use mysql to get the difference between 2 dates (DATEDIFF function):
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff

Categories