I'd like to start off by saying thank you for taking the time to read this post. I hope that someone will be kind enough to help me as I am just starting to learn PHP. Please forgive me if I do not use the correct terminology to describe my issue.
I'm having an issue sorting my array.
My array looks like this:
<?php
$rooms = array(
strtotime('next monday')=>array('day'=>'monday', 'abbrev'=>'Mon'),
strtotime('next tuesday')=>array('day'=>'tuesday', 'abbrev'=>'Tue'),
strtotime('next wednesday')=>array('day'=>'wednesday', 'abbrev'=>'Wed'),
strtotime('next thursday')=>array('day'=>'thursday', 'abbrev'=>'Thu'),
strtotime('next friday')=>array('day'=>'friday', 'abbrev'=>'Fri'),
strtotime('next saturday')=>array('day'=>'saturday', 'abbrev'=>'Sat'),
strtotime('next sunday')=>array('day'=>'sunday', 'abbrev'=>'Sun'));
ksort($rooms);
foreach($rooms as $room_timestamp=>$room_info) {
if (time() > strtotime($room_info['day'])) {
print ($form->checkBox($model,'space_'.$room_info['day'], array('value' => strtotime($room_info['day']))) . $form->labelEx($model,$room_info['abbrev'].' ' . date('n/j', strtotime($room_info['day']))) . '<br />');
} else {
print ($form->checkBox($model,'space_'.$room_info['day'], array('value' => strtotime('next '.$room_info['day']))) . $form->labelEx($model,$room_info['abbrev'].' ' . date('n/j', strtotime('next '.$room_info['day']))) . '<br />');
}
}
echo "<pre>".print_r($rooms,1)."</pre>";
?>
And it is outputting the checkboxes in this order:
Mon 6/3Tue 6/4Wed 6/5Thu 6/6Fri 6/7Sat 6/8Sun 6/2
I'm trying to get today (in this case Sun 6/2) to show first, and then the next 6 days to show in order.
When I use print_r to display the raw output, it looks like this:
Array
(
[1370239200] => Array
(
[day] => monday
[abbrev] => Mon
)
[1370325600] => Array
(
[day] => tuesday
[abbrev] => Tue
)
[1370412000] => Array
(
[day] => wednesday
[abbrev] => Wed
)
[1370498400] => Array
(
[day] => thursday
[abbrev] => Thu
)
[1370584800] => Array
(
[day] => friday
[abbrev] => Fri
)
[1370671200] => Array
(
[day] => saturday
[abbrev] => Sat
)
[1370757600] => Array
(
[day] => sunday
[abbrev] => Sun
)
)
Sunday is showing a larger time stamp than the other days, is this because it is somehow outputting next Sunday's time stamp instead of today's time stamp?
Initially I was creating the checkboxes and labels using two if/else statements for each day. Unable to sort them this way I attempted to create an array which is what I am now having trouble with.
My original code (this example is for Tuesday) looked like this:
<?php
if (time() > strtotime('tuesday'))
{
echo $form->checkBox($model,'space_tuesday', array('value' => strtotime('tuesday')));
}
else
{
echo $form->checkBox($model,'space_tuesday', array('value' => strtotime('next tuesday')));
}
?>
<?php
if (time() > strtotime('tuesday'))
{
echo $form->labelEx($model,'Tues ' . date('n/j', strtotime('tuesday')));
}
else
{
echo $form->labelEx($model,'Tues ' . date('n/j', strtotime('next tuesday')));
}
?>
Is there a better way to create an array to achieve this and sort in the correct order? Am I missing something simple with the array I've already created?
Any help that you could provide would be greatly appreciated.
Thank you.
As far as I understand it, for what you want to achieve, you're overcomplicating things:
for ( $i=0; $i<6; $i++ ) {
echo date('D n/j', strtotime("+$i day")) . '<br />';
}
Days naturally progress in the way you want, so you might as well use that fact — and avoid sorting all together. You can also make use of php's date formatting to avoid having to store shortforms of days too.
update
This should give you an idea of what you can do, I'd suggest you read up on what you can generate using PHP's date function — it's rather useful :)
http://uk1.php.net/manual/en/function.date.php
for ( $i=0; $i<6; $i++ ) {
/// pull out our datetime into a variable
$datetime = strtotime("+$i day");
/// wrap it with an array ready for checkBox()
$value = array('value' => $datetime);
/// generate the different parts we may need from date()
list($day, $formatted) = explode(',', date('l,D n/j', $datetime));
/// place those parts where we need them
echo $form->checkBox($model, "space_" . strtolower($day), $value);
echo $form->labelEx($model, $formatted));
}
Sunday is showing a larger time stamp than the other days, is this because it is somehow outputting next Sunday's time stamp instead of today's time stamp?
Yes, this is what you coded: strtotime('next sunday')
Try using numbers if you want today and next 6 days, e.g.+1 day, ..., +6 days: http://www.php.net/manual/en/dateinterval.createfromdatestring.php
Change 'next sunday' to 'sunday', put it in as the first array element and you should be good..
Related
first of all, I'm sorry for my english. I'm from germany.
Now my Problem:
I have a multiple array with some dates in it. I had to filter the first and the last date for every IP because I need the difference of both dates to know how much time the User on my website.
I did that and got all I need. Here is a part of my code output:
$ip_with_dates:
Array
(
[0] => Array
(
[ip] => 72.xx.xx.xx
[first_date] => 2015-10-12 00:10:15
[last_date] => 2015-10-12 01:10:51
)
[1] => Array
(
[ip] => 85.xx.xx.xx
[first_date] => 2015-10-12 00:10:19
[last_date] => 2015-10-12 01:10:56
)
I tried to get the time between those two dates with:
$visit_lenght = [];
foreach($ip_with_date as $key => $val){
$date1 = new DateTime($val['first_date']);
$date2 = new DateTime($val['last_date']);
$interval = $date1->diff($date2)->format('%h %m %s');
$visit_lenght[] = $interval;
}
what gives me this output:
Array
(
[0] => 1 36
[1] => 1 37
[2] => 0 3
[3] => 0 9
)
well but this isn't good to work with. I need the time in seconds not in H:m:s
but I really don't now how. This is a part of my project where I'm really fighting with. Maybe someone of you could help me with this.
I'm working with laravel. Normal PHP would make it too but if someone knows a solution in laravel, it would be nice as well!
thanks for any help!
To get time diff in seconds you need to convert your datetime objects to timestamps:
$visit_lenght = [];
foreach($ip_with_date as $key => $val){
$date1 = new DateTime($val['first_date']);
$date2 = new DateTime($val['last_date']);
$interval = $date1->getTimestamp() - $date2->getTimestamp()
$visit_lenght[] = $interval;
}
You may get timestamps of two dates and count the difference.
something like (in php).
$date1t = $date1->getTimestamp();
$date2t = $date2->getTimestamp();
$diff = $date2t - $date1t;
http://php.net/manual/en/datetime.gettimestamp.php
Try this way (general/basic way)
$visit_lenght = [];
foreach($ip_with_date as $key => $val){
$date1 = strtotime($val['first_date']);
$date2 = strtotime($val['last_date']);
//$interval = $date1->diff($date2)->format('%h %m %s');
$interval = $date2 - $date1;
$visit_lenght[] = $interval;
}
you can make this:
->format('Y-m-d H:i:s')
With DateTime() class of PHP, it is super simple to find difference between time. Here is an exmple:
<?php
$ip = "xxxx-xxx-xxx";
$t1 = DateTime::createFromFormat('Y-m-d H:i:s', '2015-10-12 00:10:15');
$t2 = DateTime::createFromFormat('Y-m-d H:i:s', '2015-10-12 01:10:51');
echo "User from IP {$ip} spent " . $t1->diff($t2)->format("%h hours, %i minutes and %s seconds");
?>
I am facing a weird issue, what I am trying to do here is to add 30 minutes to a specified time for multiple iterations using a while loop. Below is the code I am trying and not sure where it has gone wrong.
My Code
function session_slot_compare(){
$min_count = 3;
$time_now = '1:30';
$time_now_new = date('H:i', strtotime($time_now));
$duration_bits[0] = $time_now_new;
$i=0;
while($i<$min_count){
$time_now_new = date("H:i", strtotime('+30 minutes', $time_now_new));
$duration_bits[$i] = $time_now_new;
$i++;
}
print_r($duration_bits);}
Expected Output
I am actually expecting the output to be like Array ( [0] => 01:30 [1] => 02:00 [2] => 02:30 )
Actual Output
But I am getting the output as Array ( [0] => 01:30 [1] => 00:30 [2] => 00:30 )
The reason is that in the strtotime('+30 minutes', $time_now_new) you pass not a valid 2-nd argument. It should be a timestamp, but in your case it's a string. The shortest way to fix the problem is to add one more strtotime() call, like the following:
$time_now_new = date("H:i", strtotime('+30 minutes', strtotime($time_now_new)));
It'll work exactly in the way you expect.
So I have a form for creating scheduled dates.
Title, Subject, bla bla...
But then, I have a jQuery Date picker, that lets the user pick a date off a calendar.
The jQuery date picker only formats for human dates
I want to store them in UNIX TIME.
So I have this Calendar, for the YEAR, MONTH, DAY...
Then I have a standard drop down for the hour, 1:00 PM, 1:30 PM Etc...
The post print_r($_POST); looks like this,
[time] => Array
(
[0] => 5:00 PM
[1] => 1:00 PM
[2] => 8:00 PM
)
[date] => Array
(
[0] => 2014-05-08
[1] => 2014-04-04
[2] => 2014-03-28
)
I found strtotime(); for converting, human time / date into UNIX TIME, however...
How do I get array [0] from time, and date to combine and be a combined string.
There might be only 1 date, or 8 dates?!
You can iterate through your POST data and combine times:
foreach($_POST['date'] as $i => $date) {
$timestamp = strtotime($date.' '.$_POST['time'][$i]);
}
$count = count($_POST['date']); // assuming both date and time have same amount of values
for ($i = 0; $i < $count; $i++) {
$time = strtotime($_POST['date'][$i] . ' ' . $_POST['time'][$i]);
// do what you want with the time here
// Example: put all timestamps in an array.
$timestamps[] = $time;
}
I have an array of years, months and weeks.
//Returns an array containing the years, months and week numbers between two dates
function year_month($start_date, $end_date)
{
$begin = new DateTime( $start_date );
$end = new DateTime( $end_date);
$end->add(new DateInterval('P1W')); //Add 1 week to include the end date as a week
$interval = new DateInterval('P1W'); //Add 1 week
$period = new DatePeriod($begin, $interval, $end);
$aResult = array();
foreach ( $period as $dt )
{
$aResult[$dt->format('Y')][$dt->format('M')][] = "W".$dt->format('W');
}
return $aResult;
}
echo '<pre>';
print_r(year_month("25-11-2013","26-01-2014"));
echo '</pre>';
it outputs the following:
Array
(
[2013] => Array
(
[Nov] => Array
(
[0] => W48
)
[Dec] => Array
(
[0] => W49
[1] => W50
[2] => W51
[3] => W52
[4] => W01
)
)
[2014] => Array
(
[Jan] => Array
(
[0] => W02
[1] => W03
[2] => W04
[3] => W05
)
)
)
Notice that w01 is in the Dec array instead of the January array. I assume this is because Monday is the start of each week and in this case Monday is the 30th of December. Any ideas on how to get around this? In fact I am not sure what to do with border cases in general. If a week starts in one month but ends in another it should not be inserted into both months I would rather it be in the month in which it ends. But not sure how to go about this.
Use the thursday in the week of the starting date as the starting point, before adding weeks. The thursday will always be in the correct month (since that's what we use to decide if we're going to have week 53 or week 1 in the last week of December). If that thursday is in January, you're going to get W1 and January, if it's in December, you're going to get W53 and December.
If you want the month the week ends each time, use the sunday as the starting point instead.
You assume right, week "belongs" to month in which week start date is, so your output is correct.
If you wish to set month to last day in week, this can be accomplished with setISODate() method:
foreach ($period as $dt) {
$dt->setISODate($dt->format('o'), $dt->format('W'), 7);
$aResult[$dt->format('Y')][$dt->format('M')][] = "W".$dt->format('W');
}
But this will now work for every week that has dates in 2 different months, W05 is now in February.
Demo
I have some date, like:
20 November 06:10
12 November 08:12
10 October 13:23
There all in the past 6 months, Now I want to strtotime() them, but they are all un complete (lack of year), how to make some process so that I could strtotime() them?
Try this:
$dates = array("10 October 13:23", "12 November 08:12", "10 October 13:23");
foreach($dates as $d){
$exploded = explode(" ", $d);
$newDate = array_slice($exploded, 0,2,true)+array(2=>"2012")+array(3 => $exploded[2]);
//print_r($newDate);
$time = strtotime(implode($newDate));
echo $time."<br/>";
}
The output i got is:
1349868180
1352704320
1349868180
The logic is:
You lack the year, so I exploded the dates into an array to slice them, insert the year (the +array(2=>"2012") part) and glue them again with implode, and then run the strtotime.
This work only for this year, so you can use this logic to add the year to all your dates, or in the future there will be absolutely no way to filter dates from different years.
I added the dates into an array for loop through all of them, you can use the loop other ways, depending on where you have all your dates stored. For example if they are in a database you can include the script in the while($row = mysqli_fetch_assoc($result)) part where $d would be $row['date'] instead.
You should use the DateTime class and its createFromFormat and getTimeStamp methods instead of strtotime.
print_r(date_parse_from_format("d F H:i", '20 November 06:10'));
gives you:
Array
(
[year] =>
[month] => 11
[day] => 20
[hour] => 6
[minute] => 10
[second] => 0
[fraction] =>
[warning_count] => 0
[warnings] => Array
(
)
[error_count] => 0
[errors] => Array
(
)
[is_localtime] =>
)