PHP format date - php

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! ;)

Related

How to create unique code based on current date in php

I have a question, how to create/generate unique code based on current date or input date? For instance, input date = 02-02-2022, the code supposed to look like 20220202-000001.
You can create a Date object using the inegratedDateTime API.
Here you can find a list of available formatting options
THIS solution only shows one way to solve the OP's specific problem:
"I have a question, how to create/generate unique code based on current date or input date? For instance, input date = 02-02-2022, the code supposed to look like 20220202-000001.".
THIS solution does not attempt to create a UID across any/and all problem domains.
You could use str_replace() on the date to get rid of the dashes. Then use str_pad() on the count to fill it with 0's and have it "right justify" the actual count.
<?php
function makeCode($date, $count) {
$dateCode = str_replace("-", "", $date);
$countCode = str_pad(strval($count), 8, "0", STR_PAD_LEFT);
return $dateCode . "-" . $countCode;
}
// test
echo makeCode("02-02-2022", 1); // 02022022-00000001
?>
The 2nd parameter to str_pad() controls the width of the counter, how many zeroes will fill minus the string length the count intself.

Laravel Carbon cast datetime to set seconds to 00

If I have variables like this:
$arivalTime1 = '2020-06-05 15:52:27'
$arivalTime2 = '2020-06-05 15:52:55'
how can I convert these variables using Carbon to have the seconds displayed as 00:
$converted1 = 2020-06-05 15:52:00
$converted2 = 2020-06-05 15:52:00
there is many ways to do it:
1- like Vladimir verleg 's answer:
$arivalTime1 = '2020-06-05 15:52:27'
Carbon::parse($arivalTime1)->format('Y-m-d H:i:00');
2- using startOfMinute() method:
Carbon::parse($time)->startOfMinute()->toDateTimeString();
3- using create method witch gave you more control:
$value=Carbon::parse($time);
$wantedValue=Carbon::create($value->year,$value->month,$value->day,$value->hour,$value->minute,0);
Thank you #OMR for your solution given in the comments:
Carbon::parse($time)->startOfMinute()->toDateTimeString()
Parse your date-string using Carbon and format it, setting the seconds part to 00:
$arivalTime1 = '2020-06-05 15:52:27';
echo Carbon::parse($arivalTime1)->format('Y-m-d H:i:00');

how to extract only time from DATETIME column

I am trying to extract the time from each field for now what i get is:
if i use the following code:
$row["real_stime"] = $row["start_time"];
$row["real_etime"] = $row["end_time"];
i get this output:
"real_stime":"2015-11-18 07:18:00","real_etime":"2015-11-18 20:18:00"
and if i use this code:
$row["formated_start_time"] = date("H:i",$row["start_time"]);
$row["formated_end_time"] = date("H:i",$row["end_time"]);
i get this output:
"formated_start_time":"00:33","formated_end_time":"00:33"
the output that i need is:
07:18,20:18
Use H for 24 hour format and h for 12 hour format
$row["formated_start_time"] = date("H:i",strtotime$row["start_time"]));
$row["formated_end_time"] = date("H:i",strtotime$row["end_time"]));
See this link for more information on formats
See Demo Here
use date('H:i:s')
this should be
date("G:i:s",$row["start_time"])
^
change to this
date("H:i:s",$row["start_time"]);
So final well-form code is
$row["formated_start_time"] = date("H:i:s",$row["start_time"]);
$row["formated_end_time"] = date("H:i:s",$row["end_time"]);

how to delete firstdigit 0 from date and month

Code:
$today_mem = date("d.m");
echo $today_mem; // -> 15.02
I need to transform dates like 15.02 into 15.2, also need to transform for ex. 07.02 into 7.2 and so on every day.
So the question is: how to delete firstdigit 0 from date and month.
Any short solutions?
You'll want to use:
$today_mem = date("j.n");
echo $today_mem; // -> 15.2
To remove the leading zeros. See more format modifiers at: php.net/date
Use j instead of d and n instead of m:
$today_mem = date("j.n");
Reference at the PHP doc site.
use
$today_mem = date("j")

preg_match for mysql date format

im trying to validate a date to see if it matchs the mysql format
this is the code
$match = "/^\d{4}-\d{2}-\d{2} [0-2][0-3]:[0-5][0-9]:[0-5][0-9]$/";
$s = $this->input->post("report_start"). " " . $this->input->post("report_start_time").":00";
$e = $this->input->post("report_end"). " " . $this->input->post("report_end_time").":59";
if($this->input->post("action") != "")
{
echo trim($s). " => " . preg_match($match, trim($s));
echo "<br>";
echo trim($e). " => " . preg_match($match, trim($e));
}
the date format goes into $s and $e are
$s = 2011-03-01 00:00:00
$e = 2011-03-01 23:59:59
and they both return false (0).
i tested the pattern on http://www.spaweditor.com/scripts/regex/index.php and it returns true (1)
http://pastebin.com/pFZSKYpj
however if i manual inter the date strings into preg_match like
preg_match($match, "2011-03-01 00:00:00")
it works.
i have no idea what im doing wrong
======================
now that i think about it, i only need to validate the houre:min part of the datetime string.
im manually adding the seconds and the date is forced by a datepicker and users cant edit it
You're making your work harder that it needs to be. In php there are many date handling functions that mean you don't have to treat dates like strings. So, rather than test that your input dates are in the correct format, just insist on the correct format:
$adate= date_create('January 6, 1983 1:30pm'); //date format that you don't want
$mysqldate= $adate->format("Y-m-d h:i:s");//date format that you do want
There are also functions to check that a date is a real date, like checkdate.
ok heres wat i did.
since im forcing the date format and the ending seconds of the time part
i just validated the hour:mini part using "/^2[0-3]|[01][0-9]:[0-5][0-9]$";
and if that returns true i put everything together end reconstructed the final datetime string
$match = "/^2[0-3]|[01][0-9]:[0-5][0-9]$/";
$s_d = $this->input->post("report_start");
$s_t = $this->input->post("report_start_time");
$e_d = $this->input->post("report_end");
$e_t = $this->input->post("report_end_time");
if($this->input->post("action") != "")
{
if(
( preg_match($match , trim($s_d." ".$s_t.":00")) )
&& ( preg_match($match , trim($e_d." ".$e_t.":59")) )
)
{
$r = $this->model_report->client_hours_logged(array($s,$e));
$data['report'] = $r;
var_dump($r);
//$this->load->view("report/client_hours_per_client",$data);
}
}
Watch out:
[0-2][0-3] is not a good regex for hour values - it will match 01, 12, 23 and others, but it will fail 04 through 09 and 14 through 19.
Better use (2[0-3]|[01][0-9]) instead.
I use this to validate a 'Y-m-d H:i:s' format date string:
match = '/^[12][0-9]{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[01]) ([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/';
You could use strtotime and date to parse and format the date properly.
Why not just simply force the date into the format you want:
$e = '2011-03-01 00:00:00';
$mysqlFormat = date('Y-m-d H:i:s', strtotime($e));
Also, there is a bit of an error in your regex [0-2][0-3]:[0-5][0-9]:[0-5][0-9] will only match the hours of 00,01,02,03,10,11,12,13,20,21,22,23 so it will never match 4am, or 3pm among others. That aside I looked over your RegEx and I don't see any problems with it matching the test cases you've offered. I would check to make sure there is not extra whitespace on either side of date string with trim().
I concur with Tim : MySQL behaves in quirks mode and always tries to go easy on DATE and DATE_TIME column types. You can omit certain parts of your input and it still will try to compensate and achieve that goal successfully to some degree... That's why, most numbers your Reg-ex considers as invalid, MySQL will accept as valid.

Categories