Carbon::create($bulan)->lastOfMonth()->format('d')
I got this following code that search for the last day of the month with d format which return the string number "31", but i want to use the 31 as int how to convert it ?
EDIT
$tglA = Carbon::create($request->tglA)->startOfMonth()->format('Y-m-d');
$tglB = Carbon::create($request->tglB)->lastOfMonth()->format('Y-m-d');
$periodBulan = CarbonPeriod::create($tglA, '1 month', $tglB)->toArray();
$bulanInterval = [];
foreach($periodBulan as $bulan){
$temp = Carbon::create($bulan)->lastOfMonth()->format('d')->toDateTimeString();
array_push($bulanInterval, (int)$temp);
};
This is my code that i working on (already tried not using toDateTimeString still error could not be converted to int
If you get your value as a string, then you can cast it, like in this answer How do I convert a string to a number in PHP?
By simply using this code
$int = (int)$day;
in wich $day is your "31" as a string
Edit: Just checked that you already tried this solution
Related
Hello my dear coding friends.
I have a time, formatted like this 08:00:00. That time comes from my phpMyAdmin database where i have a "time" field and i get that field by using a query in my php code. The variable type of that mysqli variable containing the time is string, so i want to cut the minutes and seconds part off and turn the rest into an integer by adding (int). The code looks like this: Image of code
if (strpos ($meetings["dtStartZeit"], "0") == 0) {
$startTimeString = substr ($meetings["dtStartZeit"], 1, 1);
} else {
$startTimeString = substr ($meetings["dtStartZeit"], 0, 2);
}
$startTimeNumber = (int)$startTimeString;
Now comes the confusing part. If i have a string like this --> "8" and I want to turn it into an integer by using the above mentioned function, the result is 9 and not 8. The even more confusing part is that if I increase the value of that variable by 1, the result is 8.
Can someone explain me this please?
You don't need to use strpos or substr here. Use a single line type cast instead all of your code:
$startTimeNumber = (int) $meetings['dtStartZeit']; // "08:00:00" --> 8
To convert a string to an int you use intval()
because a string is an object of chars casting them wont ever work as expected which is what (int) is doing
I have a really simple error and I don't get why stftime PHP function returns me an empty string while all seems right with the syntax and code.
I got the following code:
$next_high_tide_date = strtotime($next_high_tide_date);
// FROM '2021-05-22 00:26:00' TO (timestamp) 1621635960
$next_high_tide_date_day = strtoupper(strftime('%A %d %B', $next_high_tide_date));
// THIS RETURNS ME THE RIGHT DAY DATE
$next_high_tide_date_hour = strtoupper(strftime('%k H %M', $next_high_tide_date));
// THIS RETURNS ME AN EMPTY STRING
My $next_hight_tide_date value after the first declaration is equal to 1621643160 (timestamp).
I checked on a strftime checker online and my code is right, but in my php page, still returns me a empty string.
Anyone has an idea ?
Maybe you should use the date() function to format your timestamp.
$next_high_tide_date = "2021-05-22 00:26:00";
$next_high_tide_date = strtotime($next_high_tide_date);
$next_high_tide_date_day = strtoupper(date('l d F', $next_high_tide_date));
$next_high_tide_date_hour = strtoupper(date('H \H i', $next_high_tide_date));
try this
or use %H instead of %k
$next_high_tide_date = "2021-05-22 00:26:00";
$next_high_tide_date = strtotime($next_high_tide_date);
$next_high_tide_date_day = strtoupper(strftime('%A %d %B',$next_high_tide_date));
$next_high_tide_date_hour = strtoupper(strftime('%H H %M',$next_high_tide_date));
I have use library chart from this page link. Unfortunately, the data I download is not compatible, for example:
I get from JSON time:
time: 1346803200
this time is not displayed on the chart. I must add three zeros at the end (look like this: 1346803200000), then the chart displays correctly. So I have code:
for ($i=0; $i < count($chart['Data']) ; $i++) {
$time = $chart['Data'][$i]['time'];
}
I need add to variable $time numeric 000 (three zeros at the end). I can not add it this way:
$time = $chart['Data'][$i]['time']."000";
because variable $time change from int to string. I must have $time in integer type. Is there any way to add three zeros without changing the variable type inside the loop ?
Not sure why you are doing this or if there is a better way, but if type conversion is the only thing that worries you, you can explicitly cast it to int:
$time = (int)($chart['Data'][$i]['time']."000");
Also, not sure if this is your desired behavior, but just note that your $time variable will get overwritten with every iteration of the for loop.
And one more thing, you can achieve your desired output without the explicit conversion by just multiplying your result with 1000, like so:
$time = $chart['Data'][$i]['time'] * 1000;
This should be a better solution than concatenation when you are working with ints
Seriously?
$time = $chart['Data'][$i]['time'] * 1000;
You con multiply for 1000
$time = $chart['Data'][$i]['time']*1000;
I have a range of dates in string format in the form of
'2014-10-12'
what i want to do is compare these dates so i can get the oldest and the youngest.
In PHP how do i convert these to a format where i can do the following?
$oldestdate;
$youngesdate;
//loop though all the dates
if($exampledate < $youngesdate)
$youesdate = $exampledate;
if($exampledate > $oldestdate)
$oldestdate = $exampledate;
Thanks
The nice thing about YYYY-MM-DD style dates is that they will always sort correctly, whether treated as text (as in your example), numbers (e.g. 20141012), or actual dates.
Thus, there's no need to do anything special to compare them as long as everything is the same format. Your code, as written, should work as-is (besides the typos for $youngestdate).
Note that if you want to do anything besides comparing them -- e.g. anything actually involving treating them like actual dates -- you will indeed want something like strtotime() or a mix of mktime() + substr()
have you tried strotime? reference http://php.net/manual/de/function.strtotime.php
then you can easily compare with <and > and so on.
have you tried checkdate(12, 31, 2000)? PHP.net Checkdate function
For years between 1 and 32767 inclusive.Check post 2 in the php.net link
You should use the DateTime class.
$arr = ['2012-10-12', '2004-10-12', '2014-08-12', '2014-09-12', '2014-09-13', '2014-09-11'];
$_now = new DateTime('now');
foreach ( $arr as $_t ) {
$d = new DateTime ( $_t );
if ( !isset($newest) || $d >= $newest ) $newest = $d;
if ( !isset($oldest ) || $d <= $oldest ) $oldest = $d;
}
echo 'Newest ' . $newest->format('Y-m-d');
echo 'Oldest' . $oldest->format('Y-m-d');
Take a look here: Reference on php.net
And here is an working example
How can I force the date format to output:
12/12/2012, 1/10/2012, 1/5/2012
instead of
12/12/2012, 01/10/2012, 01/05/2012?
My code is the following:
$adatefrom = date_create($_POST['datefrom']);
$adateto = date_create($_POST['adateto']);
$adatefrom = date_format($adatefrom, 'd/m/Y');
$adateto = date_format($adateto, 'd/m/Y');
Please do note that I have to format the date AFTER posting it.
Have a look at the PHP built in date function here
You will find that your solution is as simple as this:
date('j/n/Y',strtotime($_POST['datefrom']));
The key things to note are the characters used in the first parameter.
j represents the day without leading zeros
n represents the month without leading zeros
There are many other options you have, just have a read through the documentation.
Please note that a simple search of 'PHP date' on Google would have found this solution for you
$adatefrom = date_create($_POST['datefrom']);
$adateto = date_create($_POST['adateto']);
$adatefrom = date_format($adatefrom, 'j/n/Y');
$adateto = date_format($adateto, 'j/n/Y');
you are welcome! ;)