How to display exact date difference in php [duplicate] - java

This question already has answers here:
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
(32 answers)
Closed 8 years ago.
My Question is Little bit tricy .
i have to display date difference. (i find out date difference in terms of 2 year 7 month 3 days 5 hrs 30 min.)
Now how i display upto exact 2 higher values
please consider given case
case 1 : date difference is 0 year 2 month 21 days 7 hrs 30 min
output must be : 2 Month 21 days
case 2 : 0 year 0 month 0 days 7 hrs 20 min
output must be : 7 hours 21 days

If your date differences is in the exact form as in your question (separated with " ") and formatted as strings. This will do the trick.
<?php
function display_times($string){
$pieces = explode(" ",$string);
$num_disp = 0;
foreach($pieces as $i => $pice){
if(is_numeric($pice) && intval($pice) != 0){
echo $pice." ".$pieces[$i+1]." ";
$num_disp++;
if($num_disp >= 2) break;
}
}
}
$case1 = "0 year 2 month 21 days 7 hrs 30 min";
$case2 = "0 year 0 month 0 days 7 hrs 20 min";
display_times($case1);
echo PHP_EOL;
display_times($case2);
?>

You can modify this function. Replace 3rd parameter with $depth and modify array_slice line, like on demo.
Use examples :
echo time_diff_string('2013-05-01 00:22:35', 'now', 1), "\n";
echo time_diff_string('2013-05-01 00:22:35', 'now', 2), "\n";
echo time_diff_string('2013-05-01 00:22:35', 'now', 3), "\n";
echo time_diff_string('2013-05-01 00:22:35', 'now', 4), "\n";
Output :
6 months ago
6 months, 6 days ago
6 months, 6 days, 12 hours ago
6 months, 6 days, 12 hours, 6 minutes ago
Demo.

Related

Convert input second into Month

Months are represented by numbers ranging from 0 to 11. For example, 0 is January, 1 is February, etc.
The code would be
double mon = (sec/2592000);
But how do I control the range from 0-11?
You can use mod operation to control the range:
double mon = ((sec/2592000 - 1) % 12) // from 0 to 11

Joda time PeriodFormatterBuilder, can I omit weeks and use only days?

I have seen this, but is it possible to use only month and days, not weeks?
For example, instead of "1 month 2 weeks 2 days", I want "1 month 16 days".
You can use a solution similar to the answer you linked, but you'll also need to use a org.joda.time.PeriodType to normalized the period:
// 1 month, 2 weeks and 2 days
Period p = new Period(0, 1, 2, 2, 0, 0, 0, 0);
PeriodFormatter fmt = new PeriodFormatterBuilder()
// months
.appendMonths().appendSuffix(" month", " months").appendSeparator(" and ")
// days
.appendDays().appendSuffix(" day", " days")
.toFormatter();
System.out.println(fmt.print(p.normalizedStandard(PeriodType.yearMonthDayTime())));
This will print:
1 month and 16 days

Java convert minutes into hours minutes [duplicate]

This question already has answers here:
How to convert minutes to Hours and minutes (hh:mm) in java
(15 answers)
Closed 8 years ago.
I need to convert minutes to hours and minutes in java, so i do this
java:
long minutes = offer.getDuration();
long hours = minutes / 60;
long minnutesRemaining = minutes % 60;
trainOffer.setDuration(hours+"h"+minnutesRemaining);
output:
minutes = 129
hours = 2
minnutesRemaining = 9
how can i do for have minnutesRemaining = 09 ?
Use String.format to format the output:
System.out.println(String.format("%02d", minnutesRemaining ));

Java regex split string into different variables

I have been doing web-scraping for a project and I couldn't figure out how to split retrieved strings into different variables.
Array of strings I have retrieved
K. KAYMAKLI TSK 6 5 0 1 19 4 15 15
YEN?CAM? AK 6 4 1 1 14 7 7 13
MORMENEK?E GB 6 4 0 2 10 7 3 12
LEFKE TSK 6 3 2 1 10 8 2 11
SERDARLI GB 6 2 2 2 6 5 1 8
HAM?TKÖY ?HSK 6 2 2 2 8 8 0 8
ÇET?NKAYA TSK 6 2 2 2 6 7 -1 8
DO?AN TBSK 6 2 2 2 12 15 -3 8
YEN? BO?AZ?Ç? DSK 6 2 1 3 9 8 1 7
B. BA?CIL SK 6 1 4 1 8 9 -1 7
MA?USA TÜRK GÜCÜ 6 2 1 3 7 9 -2 7
C?HANG?R GSK 6 1 3 2 8 8 0 6
GENÇL?K GÜCÜ SK 6 1 0 5 6 14 -8 3
YALOVA SK 6 0 2 4 4 18 -14 2
I would like to put the characters until the first integer (in this case 6) into one string
and then each integer into separate variables.
eg.
String team = "YALOVA SK";
int p = 6;
int x = 0;
int y = 2;
int z = 4;
int m = 4;
int n = 18;
int k = -14;
int h = 2;
I was able to split the string by checking character by character to find the first integer and split it there and then assign each character after that to an integer. How can I solve it by using regex?
Note ?'s are turkish characters that are not displayed correctly on the console but are display correctly in the application.
The split() method on a string can be used to split the string based on a particular 'regex'. For reference on split function check Oracle java documentation link. There is also a nice tutorial about using regex in java in the Vogella website.
You can split the string based on '\s'(short white space character) or '\s+'and then use the returned array.
The syntax will be something like this.
String retrievedString = "YALOVA SK 6 0 2 4 4 18 -14 2";
String[] teamInfo=retrievedString.split("\\s");
You can use the string you retrieve from web scraping in place of "retrievedString".
Note the \\ used in the split method is to escape \.
Hope this helps...
I think Scanner class fit perfect to your needs:
Description: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

creating a number pattern with day number

Okay so I need to make a number pattern with day numbers for example: 1 - monday, 2 - tuesday, 3 - wednesday until 7 - sunday. If I put an input "n" I would get the following:
n=4
1 2 3 4
n=7
1 2 3 4 5 6 7
n=12
1 2 3 4 5 6 7 1 2 3 4 5
I've succeeded making this program if n<=14 but if n>14 I get:
n=17
1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 9 10
when it should be:
n=17
1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3
this is my code
for (x=1;x<=n;x++){
System.out.print(x+" ");
if (x==7){
for (x=1;x<=(n-7);x++)
System.out.print(x+" ");
break;
}
}
thanks in advance
Try this instead:
for (int i = 0; i < n; i++)
System.out.print(i % 7 + 1 + " ");
Whenever you want to have that "repeating" behavior, where a sequence of numbers goes up to a certain value and then restarts, use the % operator and a bit of modular arithmetic to achieve the desired effect. For n = 17 the above will print:
1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3

Categories