<?php
$dd=2021-8-28;
date_default_timezone_set('UTC');
$repl=str_replace('-', ',', $dd);
$newdat=date("Y,m,d",strtotime($repl));
echo $newdat;
?>
///output=1997,01,01 but It should be 2021,08,28
I'm not sure why you're getting 1997 in the output, honestly; when I try your code out on my machine, I get 1985, which is what I would expect -- $dd isn't a date right now, it's a subtraction problem! :) 2021-8-28 == 1985
Make $dd a string: (note the added leading zero, to make the date string ISO-compliant)
$dd="2021-08-28";
Then you can turn it into a date using the date() function; you don't need to do the string replace and can take that line out, as you've already specified to use commas as separators in the first parameter of your date() function.
$newdat=date("Y,m,d",strtotime($dd));
try this
dd='2021-8-28';
date_default_timezone_set('UTC');
$newdat=date("Y,m,d",strtotime($dd));
echo $newdat;
I am storing the date in MySQL in the format yyyy-mm-dd. I wish to extract individual elements of the date i.e. day,month and year as separate elements
How should I go about this?
I am using php
You can do it using explode function in php.
explode('-', $date_obj);
You could make use of the DateTime class which has the createFromFormat.
Like this..
<?php
$date = DateTime::createFromFormat('Y-m-d', '2009-02-23');
echo $date->format('Y');//2009
echo $date->format('F');//February or use 'M' for Feb
echo $date->format('d');//23
I have a script that saves a date like this.
$date = date('i')+50;
When I store this on my database, I got something like 1382851302 which works great to compare for past and future just using > or <.
In other part of the script for the admin I'm using a filter based on jQuery datepicker. The problem is I can't convert the date selected there to the same date() format so I can compare the date, I think time is somehow involved here (i) and well I can't.
here is my bad try.
$iniciopub = date('Y-m-d',strtotime($_POST['iniciopub']));
$finpub = date('Y-m-d',strtotime($_POST['finpub']));
This is not working.. (it just store "1970" )
if I try this..
$iniciopub = date($_POST['iniciopub']);
$finpub = date($_POST['finpub']);
I just save the day, I think because the format is like 00/00/0000 so the / is "cutting" the value because I can just save int. in the database as you can see in the format I need, I just need numbers.
Really lost on this, sorry if this is a fool question.
I'm studying so don't be so hard.
EDIT:
Ok this is my code now I'm using mktime()
$iniciodate = str_replace("/",",",$_POST['iniciopub']);
$iniciopub = mktime(0,0,0,$iniciodate);
$findate = str_replace("/",",",$_POST['finpub']);
$finpub = mktime(0,0,0,$findate);
This saves this to timestamps for 10/27/2013 = 1382824800 but the same for 10/31/2013
Also I'm using this format, I have not problem with it
dateFormat: 'mm/dd/yy',
This is now set in datepicker jQuery UI
Your variable $iniciopub has value 1970-01-01 because strtotime() returned false:
var_dump( strtotime($_POST['iniciopub']) );
Take a look at supported date and time formats. If you are saying that you have format like 00/00/0000 in $_POST, then strtotime asumes this is american month, day and year (mm/dd/yyyy), like it states here.
If inputed format 00/00/0000 is dd/mm/yyyy then the easiest method would be to use DateTime::createFromFormat or str_replace('/', '-', 'dd/mm/yyyy'); to make it european date format dd-mm-yyyy.
Question update
This code is just wrong:
$iniciodate = str_replace("/",",",$_POST['iniciopub']);
$iniciopub = mktime(0,0,0,$iniciodate);
mktime() has 6 parameters (7th is is_dst wthich doesn't matter now). You cannot use $iniciodate like that, because you are inputing only 4th parameter into mktime() function, and not 4th, 5th and 6th as you might think.
mktime(0,0,0,'10,27,2013'); is not the same as mktime(0,0,0,10,27,2013); or `mktime(0,0,0,'10','27','2013');. The reason why date 10/27/2013 and 10/31/2013 return same timestamp is because in both cases, when you cast string 10,27,2013 and 10,31,2013 to integer (4th parameter), you get 10. See:
$v = '10,27,2013'; var_dump( (int)$v ); # int(10)
$v = '10,31,2013'; var_dump( (int)$v ); # int(10)
And because of that, your call is the same if you would call mktime() like:
var_dump( mktime(0,0,0,10) ); # int(1382832000)
If you would have error_reporting on (put error_reporting(-1); on the beggining of the php file), you would see, that after calling mktime(0,0,0,$iniciodate) you would get NOTICE:
Notice: A non well formed numeric value encountered in file.php on line XX
Since 10/27/2013 and 10/31/2013 are standard american date formats, you can just use strtotime() instead of mktime() to convert date to timestamps:
var_dump( strtotime('10/27/2013') ); # int(1382832000)
var_dump( strtotime('10/31/2013') ); # int(1383177600)
You asked in the comments: if i store a date as dd-mm-yyyy. how can i compare the date with other date. am i doing fine storing as timestamp?
Yes, you can store them as timestamp. But I haven't used timestamp in my code for ages, I am storing dates as YYYY-MM-DD; in RDBMS this is kinda best practice.
Better way not conver to mktime in PHP - save data as mktime already:
function mdf_init_calendars() {
jQuery(".mdf_calendar").datepicker(
{
showWeek: true,
firstDay: 1,
changeMonth: true,
changeYear: true,
showButtonPanel: true,
onSelect: function(dateText, self) {
var date = new Date(parseInt(self.currentYear, 10), parseInt(self.currentMonth, 10), parseInt(self.currentDay, 10), 23, 59, 59);
var mktime = (date.getTime() / 1000);
jQuery(this).prev('input[type=hidden]').val(mktime);
}
}
);
jQuery(".mdf_calendar").datepicker("option", "dateFormat", 'dd-mm-yy');
jQuery(".mdf_calendar").datepicker("option", "showAnim", 'fadeIn');
//+++
jQuery(".mdf_calendar").each(function() {
var mktime=jQuery(this).prev('input[type=hidden]').val();
var date = new Date(mktime*1000);
jQuery(this).datepicker('setDate', new Date(date));
});
}
You saves data in hidden inputs, and load in them when page loading, so in PHP code you get mktime always and do not need to think about data format, you can set it in js as you want (in example inline)
PHP:
How would you convert the date-time coming from an SQL server into another format? The value coming from the SQL database is like 2013-09-23 12:20:30 and I want it to be displayed like Sept-23 12:30 pm.
I can send the date-time to server in this format by PHP, but then I will have a problem when I need to compare the date-time in SQL. The field in the database is set to the datetime type.
I've tried:
<?php
if(substr($postainarray['time'],8,2) == $CurrDate) {
$timetobeshown=substr($postainarray['time'],11,5);
} else {
$timetobeshown=substr($postainarray['time'],5,11);
}
?>
<?=$timetobeshown?>
$currDate is today's date.
$postinarray is the array after the MySQL query.
After this code I get the desired format but not in desired style. If the date matches I get something like 14:00, otherwise I get 09-24 18:09, and I want it to be like Sept-24 6:09 pm.
$a='2013-09-23 12:20:30';
echo date("M d g:i a",strtotime($a));
for date format manipulation, the reference page is: PHP:date Manual
there you can checkout all the options you might need, and for your case it will be:
echo date("M-d g:i a", certaintaime);
Try this,
<?php
$date_from_db='2013-09-23 12:20:30';
echo date("M-d g:i a",strtotime($date_from_db));
?>
i have a strange questions.
In my php page i try to print dates with php function and javascript function.
My code is:
// 04 09 2013 09:47:28
<script>document.write(new Date());</script>
// 04 09 2013 09:48:17
<?php echo date('d m Y H:i:s');?>
Why the dates are not equal, but there is a litte second of difference?
I would have same dates beetween php and javascript.
---UPDATE CODE---
function startCounter(){
start = new Date(<?php echo time(); ?> * 1000);
end = new Date(<?php echo $end_ts; ?> * 1000);
timer = setInterval(updateCounter, refreshInterval);
}
function updateCounter(){
var now = new Date();
var distance = new Date(end - now);
}
Thank you very much.
First of all you need to understand the time being printed by php is the server time and time being printed by javascript is your local computer time. If the time between those 2 is different then it can show different time.
Like others said, javascript time is client time and php time is server time.
To solve the problem try something simliar to:
<? $time = time(); ?>
<script>document.write(new Date(<?=$time*1000?>));</script>
<?=date('Y-m-d H:i:s', $time')?>