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")
Related
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"]);
Test case scenario - User clicks on one of two links: 2012/10, or 2012/10/15.
I need to know whether the DAY is specified within the link. I am already stripping the rest of the link (except above) out of my URL, am I am passing the value to an AJAX request to change days on an archive page.
I can do this in either JS or PHP - is checking against the regex /\d{4}\/\d{2}\/\d{2}/ the only approach to seeing if the day was specified or not?
You can also do this if you always get this format: 2012/10 or 2012/10/15
if( str.split("/").length == 3 ) { }
But than there is no guaranty it will be numbers. If you want to be sure they are numbers you do need that kind of regex to match the String.
You could explode the date by the "/" delimiter, then count the items:
$str = "2012/10";
$str2 = "2012/10/5";
echo count(explode("/", $str)); // 2
echo count(explode("/", $str2)); // 3
Or, turn it into a function:
<?php
function getDateParts($date) {
$date = explode("/", $date);
$y = !empty($date[0]) ? $date[0] : date("Y");
$m = !empty($date[1]) ? $date[1] : date("m");
$d = !empty($date[2]) ? $date[2] : date("d");
return array($y, $m, $d);
}
?>
I would personally use a regex, it is a great way of testing this sort of thing. Alternatively, you can split/implode the string on /, you will have an array of 3 strings (hopefully) which you can then test. I'd probably use that technique if I was going to do work with it later.
The easiest and fastest way is to check the length of the string!
In fact, you need to distinguish between: yyyy/mm/dd (which is 10 characters long) and yyyy/mm (which is 7 characters).
if(strlen($str) > 7) {
// Contains day
}
else {
// Does not contain day
}
This will work EVEN if you do not use leading zeros!
In fact:
2013/7/6 -> 8 characters (> 7 -> success)
2013/7 -> 6 characters (< 7 -> success)
This is certainly the fastest code too, as it does not require PHP to iterate over the whole string (as using explode() does).
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! ;)
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.
Hey, I need som help to list my added dates from database, and split it into their added month.
I have no clue on how to do it... Soe can someone please show me examples, or maybe some tutorials how to do?
Thx
Something along the lines of this, perhaps?
SELECT * FROM table GROUP BY MONTH(dateColumn)
SELECT * FROM table WHERE MONTH(dateColumn) = 9
A must-read reference for date & time handling functions in MySQL is:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
Are you looking for the MySQL MONTH() Function?
Query: SELECT MONTH(NOW());
Output: 11
Not entirely sure what you mean but here goes...
The sample below creates a test collection (assumably your collection retrieved from the database) and groups them by Month and Year and then displays the result. It uses Linq and anonymous objects which you could easily replace with some POCO classes...
Sub Main()
Dim ls As New List(Of Object)
Dim lsGroup As New List(Of Object)
Dim ran As New Random(Now.Millisecond)
'' build a sample collection
For x As Integer = 1 To 100
ls.Add(New With {.ID = x, .DateAdded = Now.AddMinutes(-(ran.Next(1, 100000)))})
Next
'' now group them into years and months
For Each item In ls
Dim currentItem As Object = lsGroup.Where(Function(o) o.Year = item.DateAdded.Year And o.Month = item.DateAdded.Month).SingleOrDefault()
If currentItem Is Nothing Then
'' create
Dim var = New With {.Year = item.DateAdded.Year, .Month = item.DateAdded.Month, .ItemCollection = New List(Of Object)}
var.ItemCollection.Add(item)
lsGroup.Add(var)
Else
'' add
currentItem.ItemCollection.Add(item)
End If
Next
'' display the results
For Each group In lsGroup
Console.WriteLine(group.Year & " - " & MonthName(group.Month))
For Each item In group.ItemCollection
Console.WriteLine(" > " & item.ID & " - " & item.DateAdded.ToString())
Next
Console.WriteLine()
Next
Console.ReadLine()
End Sub
Here's what I do when I need the month that's in a timestamp or date item called "t".
TIMESTAMP(DATE_FORMAT(t,'%y-%m-01'))
This returns another timestamp that represents midnight on the first day of that month.
Works for weeks too.
TIMESTAMP(FROM_DAYS(TO_DAYS(t) -MOD(TO_DAYS(t) -1, 7)))
This obscure incantation returns a timestamp that represents midnight on the Sunday preceding the given timestamp.